日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Python - Windows系统下安装使用virtualenv

發布時間:2025/3/20 windows 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python - Windows系统下安装使用virtualenv 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 - virtualenv

https://pypi.python.org/pypi/virtualenv/
https://github.com/pypa/virtualenv

在實際開發測試中,每個應用很可能需要不同的Python開發和運行環境、引用不同的依賴、設置不同的權限。
隨著應用的增多,不同應用間必然會產生依賴沖突、Python版本沖突以及間接權限問題。
利用virtualenv工具可以針對每個應用創建一套“隔離的、純凈的”Python開發和運行環境,能夠獨立地使用Python環境和管理庫,從而很好地解決以上問題。
創建并激活一個virtualenv環境時,virtualenv會修改相關環境變量,讓命令python和pip均指向當前的virtualenv環境。
創建虛擬環境的過程,實際上是將系統Python環境復制成為一個擁有獨立安裝目錄的虛擬Python環境,這個環境不影響系統Python環境,也不影響其他虛擬環境。

2 - 安裝使用virtualenv

當前環境描述:Window7-x64系統同時安裝了Python2.7和Python3.6,并且將Python2.7作為主環境,兩個版本各自都已安裝很多第三方庫。

Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved.C:\Users\guowli>py -2 -V Python 2.7.12C:\Users\guowli>py -3 -V Python 3.6.0C:\Users\guowli>python -V Python 2.7.12C:\Users\guowli>

2.1 - 安裝virtualenv

1- pip安裝

C:\Users\guowli>pip install virtualenv --proxy="10.144.1.10:8080" Collecting virtualenvUsing cached virtualenv-15.1.0-py2.py3-none-any.whl Installing collected packages: virtualenv Successfully installed virtualenv-15.1.0C:\Users\guowli>pip show virtualenv Name: virtualenv Version: 15.1.0 Summary: Virtual Python Environment builder Home-page: https://virtualenv.pypa.io/ Author: Jannis Leidel, Carl Meyer and Brian Rosner Author-email: python-virtualenv@groups.google.com License: MIT Location: c:\python27\lib\site-packages Requires:C:\Users\guowli>

注意:在Python3的環境中使用“pip3 install virtualenv --proxy="10.144.1.10:8080"命令安裝和使用“pip3 show virtualenv”命令查看包信息。

2- 源碼安裝

  • 下載并解壓virtualenv源碼壓縮包(https://pypi.python.org/pypi/virtualenv/),例如:virtualenv-15.1.0.tar.gz。
  • 在解壓目錄(例如:virtualenv-15.1.0)下的命令行界面,執行安裝命令:python setup.py install。

2.2 - 創建虛擬環境

注意:

  • 創建虛擬環境可能需要幾分鐘,請耐心等待。
  • 參數“-p”:指定虛擬環境的Python解釋器版本;需使用絕對路徑。
  • 參數“--no-site-packages”:創建“純凈”的Python運行環境(已安裝的第三方包不復制到虛擬環境)。
  • 參數“--system-site-packages”: 當前系統Python環境的所有庫也會安裝在虛擬環境。
  • 創建虛擬環境成功后,會生成對應名稱的目錄文件。系統類型不同,目錄文件也略有區別。
  • 創建的虛擬環境不能跨平臺使用。

示例: 創建名稱為VenPy3、解釋器為Python3.6、純凈的(沒有任何第三方包)虛擬環境。

D:\Anliven-Running\Zen\TestEnvPython>virtualenv -p c:\Python36\python.exe --no-site-packages VenvPy3 Running virtualenv with interpreter c:\Python36\python.exe Using base prefix 'c:\\Python36' New python executable in D:\Anliven-Running\Zen\TestEnvPython\VenvPy3\Scripts\python.exe Installing setuptools, pip, wheel...done.D:\Anliven-Running\Zen\TestEnvPython>dirVolume in drive D is DATAVolume Serial Number is 3479-3F1EDirectory of D:\Anliven-Running\Zen\TestEnvPython2017/12/05 16:37 <DIR> . 2017/12/05 16:37 <DIR> .. 2017/12/05 16:37 <DIR> VenvPy30 File(s) 0 bytes3 Dir(s) 76,200,370,176 bytes freeD:\Anliven-Running\Zen\TestEnvPython>source 'source' is not recognized as an internal or external command, operable program or batch file.D:\Anliven-Running\Zen\TestEnvPython>dir VenvPy3\Volume in drive D is DATAVolume Serial Number is 3479-3F1EDirectory of D:\Anliven-Running\Zen\TestEnvPython\VenvPy32017/12/05 16:37 <DIR> . 2017/12/05 16:37 <DIR> .. 2017/02/13 10:59 <DIR> Include 2017/12/05 16:37 <DIR> Lib 2017/12/05 16:43 <DIR> Scripts 2017/12/05 16:37 <DIR> tcl0 File(s) 0 bytes6 Dir(s) 76,200,370,176 bytes freeD:\Anliven-Running\Zen\TestEnvPython>

目錄解釋
? ?- lib:所有python庫的安裝位置為“lib/pythonx.x/site-packages/”目錄;inux系統對應為“bin”目錄。
? ?- bin:bin/python是在當前環境使用的python解釋器

2.3 - 激活虛擬環境

虛擬環境Script目錄下的activate腳本用來激活當前虛擬環境,可直接執行deactivate關閉(去激活)當前虛擬環境。
注意:激活后,

  • 在虛擬環境下,用pip安裝的包都被安裝到這個環境,系統Python環境不受任何影響。
  • 命令行提示符前,將顯示生效的虛擬環境名稱。
  • 虛擬環境作用于當前終端的所有目錄;關閉當前終端,虛擬環境也同時關閉。
  • deactivate命令可以在任意目錄直接執行。
D:\Anliven-Running\Zen\TestEnvPython\VenvPy3\Scripts>dirVolume in drive D is DATAVolume Serial Number is 3479-3F1EDirectory of D:\Anliven-Running\Zen\TestEnvPython\VenvPy3\Scripts2017/12/05 16:43 <DIR> . 2017/12/05 16:43 <DIR> .. 2017/12/05 16:43 2,223 activate 2017/12/05 16:43 783 activate.bat 2017/12/05 16:43 8,325 activate.ps1 2017/12/05 16:43 1,137 activate_this.py 2017/12/05 16:43 508 deactivate.bat 2017/12/05 16:42 98,194 easy_install-3.6.exe 2017/12/05 16:42 98,194 easy_install.exe 2017/12/05 16:42 98,166 pip.exe 2017/12/05 16:42 98,166 pip3.6.exe 2017/12/05 16:42 98,166 pip3.exe 2017/12/05 16:37 100,504 python.exe 2017/12/05 16:37 3,555,992 python36.dll 2017/12/05 16:37 98,968 pythonw.exe 2017/12/05 16:42 98,173 wheel.exe14 File(s) 4,357,499 bytes2 Dir(s) 76,200,140,800 bytes freeD:\Anliven-Running\Zen\TestEnvPython\VenvPy3\Scripts>activate(VenvPy3) D:\Anliven-Running\Zen\TestEnvPython\VenvPy3\Scripts>python -V Python 3.6.0(VenvPy3) D:\Anliven-Running\Zen\TestEnvPython\VenvPy3\Scripts>cd ../../../(VenvPy3) D:\Anliven-Running>pip list DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define aformat=(legacy|columns) in your pip.conf under the [list] section) to disable this warning. pip (9.0.1) setuptools (28.8.0) wheel (0.29.0)(VenvPy3) D:\Anliven-Running>deactivate D:\Anliven-Running>python -V Python 2.7.12D:\Anliven-Running>

注意:Linux系統下,激活命令類似于“source ./bin/activate”

2.4 - 在虛擬環境中安裝依賴

使用pip命令安裝依賴,操作與系統Python環境相同。
依賴會安裝到“lib/pythonx.x/site-packages/”目錄;Linux系統對應為“bin”目錄下。
可以使用-r requirements.txt批量安裝依賴

pip freeze #顯示所有已安裝的依賴 pip freeze > requirement.txt #生成包含所有已安裝的依賴信息的requirement.txt文件 pip install -r requirement.txt #批量安裝依賴

2.5 - 刪除虛擬環境

直接刪除虛擬環境目錄和文件。

3 - virtualenv幫助信息

D:\Anliven-Running\Zen>virtualenv -h Usage: virtualenv [OPTIONS] DEST_DIROptions:--version show program's version number and exit-h, --help show this help message and exit-v, --verbose Increase verbosity.-q, --quiet Decrease verbosity.-p PYTHON_EXE, --python=PYTHON_EXEThe Python interpreter to use, e.g.,--python=python2.5 will use the python2.5 interpreterto create the new environment. The default is theinterpreter that virtualenv was installed with(c:\python27\python.exe)--clear Clear out the non-root install and start from scratch.--no-site-packages DEPRECATED. Retained only for backward compatibility.Not having access to global site-packages is now thedefault behavior.--system-site-packagesGive the virtual environment access to the globalsite-packages.--always-copy Always copy files rather than symlinking.--unzip-setuptools Unzip Setuptools when installing it.--relocatable Make an EXISTING virtualenv environment relocatable.This fixes up scripts and makes all .pth filesrelative.--no-setuptools Do not install setuptools in the new virtualenv.--no-pip Do not install pip in the new virtualenv.--no-wheel Do not install wheel in the new virtualenv.--extra-search-dir=DIRDirectory to look for setuptools/pip distributions in.This option can be used multiple times.--download Download preinstalled packages from PyPI.--no-download, --never-downloadDo not download preinstalled packages from PyPI.--prompt=PROMPT Provides an alternative prompt prefix for thisenvironment.--setuptools DEPRECATED. Retained only for backward compatibility.This option has no effect.--distribute DEPRECATED. Retained only for backward compatibility.This option has no effect.D:\Anliven-Running\Zen>

4 - 安裝使用virtualenvwrapper-win

https://pypi.python.org/pypi/virtualenvwrapper
https://pypi.python.org/pypi/virtualenvwrapper-win
virtualenvwrapper是virtualenv的一層封裝,提供了一些便利命令行,適用于Linxu系統。
virtualenvwrapper-win適用于Windows系統。
可以直接安裝virtualenvwrapper-win,同時virtualenv作為依賴也將被安裝。

4.1-安裝virtualenvwrapper-win

D:\Anliven-Running\Zen>pip install virtualenvwrapper-win --proxy="10.144.1.10:8080" Collecting virtualenvwrapper-winDownloading virtualenvwrapper_win-1.2.4-py2-none-any.whl Requirement already satisfied: virtualenv in c:\python27\lib\site-packages (from virtualenvwrapper-win) Installing collected packages: virtualenvwrapper-win Successfully installed virtualenvwrapper-win-1.2.4D:\Anliven-Running\Zen> D:\Anliven-Running\Zen>pip show virtualenvwrapper-win Name: virtualenvwrapper-win Version: 1.2.4 Summary: Port of Doug Hellmann's virtualenvwrapper to Windows batch scripts Home-page: https://github.com/davidmarble/virtualenvwrapper-win/ Author: David Marble Author-email: davidmarble@gmail.com License: BSD 3-clause Location: c:\python27\lib\site-packages Requires: virtualenvD:\Anliven-Running\Zen>

4.2-設置環境變量

virtualenvwrapper-win默認虛擬環境目錄指向用戶文件夾的Envs目錄,需要改為實際的虛擬環境保存目錄。

D:\Anliven-Running\Zen\TestEnvPython>lsvirtualenvdir /b /ad "C:\Users\guowli\Envs" ============================================================================== File Not FoundD:\Anliven-Running\Zen\TestEnvPython>

控制面板--->系統--->屬性--->高級系統設置--->環境變量。新建系統變量并保存,虛擬環境的保存路徑WORKON_HOME,重啟終端。

C:\Users\guowli>lsvirtualenvdir /b /ad "D:\Anliven-Running\Zen\TestEnvPython" ============================================================================== testPy3.6 VenvPy3C:\Users\guowli>

4.3-常用命令及示例

Main Commands : https://pypi.python.org/pypi/virtualenvwrapper-win

lsvirtualenv # 列出WORKON_HOME目錄下已有的虛擬環境 mkvirtualenv <name> # 創建虛擬環境 rmvirtualenv <name> # 刪除虛擬環境 workon <name> # 激活虛擬環境,在不同的虛擬環境間切換 deactivate # 關閉(去激活)虛擬環境

mkvirtualenv幫助信息:

C:\Users\guowli>mkvirtualenv --helpUsage: mkvirtualenv [mkvirtualenv-options] [virtualenv-options] DEST_DIRDEST_DIR The name of the envirnment to create (must be last).The new environment is automatically activated after being initialized.mkvirtualenv options:-a project_path Associate existing path as project directory-i package Install package in new environment. This optioncan be repeated to install more than one package.-r requirements_file requirements_file is passed topip install -r requirements_fileNOTE: all mkvirtualenv-options must come before virtualenv-options!

示例:

C:\Users\guowli>mkvirtualenv -p c:\Python27\python.exe --no-site-packages testPy2.7 Running virtualenv with interpreter c:\Python27\python.exe New python executable in D:\Anliven-Running\Zen\TestEnvPython\testPy2.7\Scripts\python.exe Installing setuptools, pip, wheel...done.(testPy2.7) C:\Users\guowli>pip list DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define aformat=(legacy|columns) in your pip.conf under the [list] section) to disable this warning. pip (9.0.1) setuptools (28.8.0) wheel (0.29.0)(testPy2.7) C:\Users\guowli>deactivateC:\Users\guowli> C:\Users\guowli>lsvirtualenvdir /b /ad "D:\Anliven-Running\Zen\TestEnvPython" ============================================================================== testPy2.7 testPy3.6 VenvPy3C:\Users\guowli> C:\Users\guowli>rmvirtualenv VenvPy3Deleted D:\Anliven-Running\Zen\TestEnvPython\VenvPy3C:\Users\guowli> C:\Users\guowli>workon testPy2.7 (testPy2.7) C:\Users\guowli> (testPy2.7) C:\Users\guowli>workon testPy3.6 (testPy3.6) C:\Users\guowli> (testPy3.6) C:\Users\guowli>deactivate C:\Users\guowli>

5 - 在PyCharm中使用virtualenv(推薦)

PyCharm本身已集成virtualenv工具,無需手工安裝,可以創建并為項目設置指定的虛擬環境。
PyCharm文檔:https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html#configuring-venv

創建方式一:新建項目時創建虛擬環境
File--》New Project--》Pure Python--》點擊Interpreter參數欄右側齒輪圖標并選擇“Create VirtualEnv”--》根據選擇填寫Name、Location和Base interpreter。

創建方式二:在當前項目創建虛擬環境
File--》Settings--》Project:xxx--》Project Interpreter,點擊參數欄右側齒輪圖標并選擇“Create VirtualEnv”--》根據選擇填寫Name、Location和Base interpreter。
勾選“Make available to all projects”參數,可以使虛擬環境都所有項目都起作用。
勾選“Inherit global site-packages”參數,將繼承當前系統Python環境的所有庫。

6 - 其他

Linux下安裝使用virtualenv和virtualenvwrapper的方法與Windows基本一致,只是在環境變量設置和激活虛擬環境命令上有細微差別。

轉載于:https://www.cnblogs.com/anliven/p/7995301.html

總結

以上是生活随笔為你收集整理的Python - Windows系统下安装使用virtualenv的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。