程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

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

  1. pip install virtualenv(pip If not , Find it online , Not much introduction )

pip install virtualenv Failure resolution

  1. 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
 D:\pyvenv_a>pip install virtualenv
Collecting virtualenv
Downloading https://files.pythonhosted.org/packages/33/5d/314c760d4204f64e4a968275182b7751bd5c3249094757b39ba987dcfb5a/virtualenv-16.4.3-py2.py3-none-any.whl (2.0MB)
  1. 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

  1. 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 ;
  2. call virtualenv --no-site-packages venv Automatically create a venv python Environmental Directory
  3. –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

  1. 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

D:\pyvenv_a>cd venv\Scripts\
D:\pyvenv_a\venv\Scripts>activate
(venv) D:\pyvenv_a\venv\Scripts>

pyinstaller Installation method

  1. Because my new environment is for pyinstaller pack exe To prepare for , All are also attached pyinstaller Installation method
  2. 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

  1. 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
(venv) D:\pyvenv_a\venv>where pyinstaller
F:\Program_File\anaconda3\Scripts\pyinstaller.exe
(venv) D:\pyvenv_a\venv>
  1. 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
  2. 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
(venv) D:\pyvenv_a\venv>where pyinstaller
D:\pyvenv_a\venv\Scripts\pyinstaller.exe
F:\Program_File\anaconda3\Scripts\pyinstaller.exe
(venv) D:\pyvenv_a\venv>

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved