Use virtualenv to build a local Python venv environment and isolate the anaconda + pyinstaller installation method
編輯:Python
List of articles
background
problem
step
Confirm the installation virtualenv
install virtualenv
pip install virtualenv Failure resolution
Create a venv python Environmental Science
Start a new environment
pyinstaller Installation method
matters needing attention
background
anaconda It's an open source Python Release version , It contains conda、Python etc. 180 Multiple science packs and their dependencies . [1] Because it contains a lot of science packages ,Anaconda The download file of is relatively large ( about 531 MB);
pycharm You can use it directly anaconda As a parser environment
problem
Sometimes we need to develop and maintain multiple projects , The use of python Versions and packages vary
Or we need to use pyinstaller pack win Program , Avoid packaged programs that are too large ( super 300Mb)
To solve this problem pyinstaller Packaging failure and package exe Too big
This is the time , The best version is to create a new pure python venv Environmental Science
step
Confirm the installation virtualenv
open cmd window , Input virtualenv -h Confirm whether the existing environment has virtualenv, If there is one, it is shown as follows , Install if not virtualenv
C:\Users\wangshengke>virtualenv -h
Usage: virtualenv [OPTIONS] DEST_DIR
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose Increase verbosity.
......
install virtualenv
pip install virtualenv(pip If not , Find it online , Not much introduction )
pip install virtualenv Failure resolution
If the installation fails due to timeout or other reasons , You can manually according to the address With a browser Download installation package ; then pip install virtualenv-16.4.3-py2.py3-none-any.whl that will do
You can also use domestic image servers , Avoid foreign server timeout for example :pip install -i https://pypi.tuna.tsinghua.edu.cn/simple virtualenv, In this way, you can install the image from Tsinghua virtualenv library
Create a venv python Environmental Science
According to one's hobbies , Create in the corresponding directory , First of all cd Go to the corresponding directory , For example, I was d:\pyenv_a Folder to create a virtual environment ;
–no-site-packages It means not using global site-packages package , prevent anaconda The package inherited from the pollution
D:\pyvenv_a> virtualenv --no-site-packages venv
Using base prefix 'F:\\Program_File\\anaconda3'
New python executable in D:\pyvenv_a\venv\Scripts\python.exe
copying F:\Program_File\anaconda3\python.exe => D:\pyvenv_a\venv\Scripts\python.exe
Installing setuptools, pip, wheel...done.
D:\pyvenv_a>
Start a new environment
cd To D:\pyvenv_a>cd venv\Scripts Next , call activate You can enable the new environment variable ,cmd A... Will appear before the command (venv), Mark that the switch is successful
Because my new environment is for pyinstaller pack exe To prepare for , All are also attached pyinstaller Installation method
Call directly pip install pyinstaller There is a timeout socket.timeout: The read operation timed out
(venv) D:\pyvenv_a>pip install pyinstaller
Collecting pyinstaller
Downloading https://files.pythonhosted.org/packages/03/32/0e0de593f129bf1d1e77eed562496d154ef4460fd5cecfd78612ef39a0cc/PyInstaller-3.4.tar.gz (3.5MB)
Exception:
v = self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out
Solution : According to the address above Manual Download https://files.pythonhosted.org/packages/03/32/0e0de593f129bf1d1e77eed562496d154ef4460fd5cecfd78612ef39a0cc/PyInstaller-3.4.tar.gz Then put it in the current directory , call pip install PyInstaller-3.4.tar.gz install (venv) D:\pyvenv_a\venv>pip istall PyInstaller-3.4.tar.gz
matters needing attention
After creating a new environment according to the above , If you want to use any tool or package , All need new installation , It's best to use the command to confirm whether you are using the tools of the new environment before each call 、 package
for example : I haven't installed pyinstaller, If called directly , The system did not report an error , But led me to anaconda Installation of , What led me to finally use anaconda Environment , There's no isolation , There are various problems
After installation , It will become the following paths in this environment , Then the environment will give priority to its own , Don't look for anaconda