文章目錄
- 前言:
- 1、下載安裝virutalenv
- 2、安裝新的Python版本(可以直接安裝anaconda:安裝過程可自行查資料)
- 3、 VirtualEnv的設置
- 4、Python虛擬環境的作用
- 總結:
前言:
在目前的Linux系統中,默認使用的都是Python 2.7.x的版本環境,如何想要在其上基于Python3的環境來進行開發,則需要基于VirutalEnv來構建獨立的開發環境。
1、下載安裝virutalenv
執行pip命令
>> pip install virtualenv
[root@xxxZ]# pip install virtualenv
Collecting virtualenvDownloading http://mirrors.aliyun.com/pypi/packages/6f/86/3dc328ee7b1a6419ebfac7896d882fba83c48e3561d22ddddf38294d3e83/virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)100% |████████████████████████████████| 1.8MB 34.9MB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
測試是否安裝成功:
>> virtualenv -h
[root@xxx]# 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(/usr/bin/python2)--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.
virutalenv安裝成功了
2、安裝新的Python版本(可以直接安裝anaconda:安裝過程可自行查資料)
可以從python的官方站點,下載源代碼,編譯安裝,這里Python安裝包是3.6.1的版本。具體的安裝過程可以參考網路上的文檔。
* download the source code package from python.org* tar zxvf python.xx.tgz , tar xvf Python.tar.xz* cd source_code_folder* ./configure --prefix=/opt/python-install-path* make & make install
默認會安裝到/usr/local
3、 VirtualEnv的設置
3.1、對于安裝Python的情況:
>> virtualenv -p python36-install-path/bin/python.exe python36env這里設置Python版本為3.6, 新的環境目錄為python36env>> source python36env/bin/activate # 激活虛擬環境>> deactivate # 退出當前的虛擬環境
3.2、對于安裝anaconda的情況:
大部分情況下,我們希望直接用anaconda中的python環境以及anaconda自帶的許多第三方庫,這時又不想和別的python程序共用環境。這時需要自定義一個虛擬環境。過程如下:
1、為Anaconda創建一個python3.5(或者3.6都行,隨便)的環境,環境名稱為test ,輸入下面命令:
conda create -n test
2、啟動test環境
source activate test
#或者 conda activate test
關閉test環境,命令為:
source deactivate test
#或者 conda deactivate test
3、安裝所需的各種第三方庫
(tensorflow)$ pip install xxx
4、測試安裝的第三方庫
4、Python虛擬環境的作用
Python的虛擬環境可以使一個Python程序擁有獨立的庫library和解釋器interpreter,而不用與其他Python程序共享統一個library和interpreter。虛擬環境的好處是避免了不同Python程序間的互相影響(共同使用global library 和 interpreter),例如程序A需要某個庫的1.0版本,而程序B需要同樣這個庫的2.0版本,如果程序B執行則A就不能執行了。
總結:
這個隔離的環境非常方便,進行程序的開發和環境的切換。
參考:https://blog.csdn.net/blueheart20/article/details/70598031
參考:https://blog.csdn.net/u012052268/article/details/77487604