Python Virtual Environment virtualenv installation and configuration

Source: Internet
Author: User
Tags virtual environment virtualenv install matplotlib

http://blog.csdn.net/pipisorry/article/details/47008981

python virtual Environment virtualenv

Virtualenv is used to create multiple standalone Python runtime environments on a single machine, virtualenvwrapper provides some handy package on the command line.

Virtualenv is a very good virtual Python environment builder, and his greatest benefit is that it allows each Python project to use one environment alone without affecting the Python system environment or the environment of other Projects.

Virtualenv can be used to create a standalone Python environment in which different Python versions or different packages are available, and you can install new packages in the environment without root privileges, with no effect on each other.


Why do you use virtualenv

-isolation of Third-party package dependencies between projects, such as a project dependent on Django1.2.5,b project dependent django1.3.

-to facilitate the deployment of applications, the development environment of the virtual environment packaged in a production environment, do not need to be on the server again. on the server do not install virtualenv, directly copy the directory created by Virtualenv to the server, modify the path, the virtual environment can be migrated .

-you can also use a Python environment without root privileges, if you do not have root privileges, you can start with a virtualenv and then install the PIP in Virtualenv. (there is no pip in the system and there is no root permission to install with sudo apt-get)

The location of the installed libraries is in the env/lib/site-packages/directory, not in the Python lib/site-packages directory of the system, so you know why the virtual environment is Separate.

note:The virtual environment created by Virtualenv is completely unrelated to the Host's Python environment, and your host-configured libraries cannot be used directly in Virtualenv. You need to install the configuration again using Pip install in a virtual environment before you can use it.

Phi Blog



installation of Virtualenv

Note: Already installed Python3 This step may not, Python3 comes with pyvenv, with this on line, the former virtualenv MyVenv--python=python3 always error

PIP installation virtualenv

Pip Install virtualenv #py2安装

PIP3 install virtualenv #py3安装, so virtualenv created with virtualenv by default is to use Py3

Two new subdirectories will appear under the Python3.4.2\lib\site-packages directory virtualenv_support, virtualenv-13.1.0.dist-info

Installing Virtualenvwrapper

[some Common Virtualenvwrapper commands may need to be mastered to facilitate the management of Python versions, linked in:
http://www.doughellmann.com/docs/virtualenvwrapper/command_ref.html]

After installation, create a virtual environment, and then install the Virtualenvwrapper
virtualenv ENV #ENV for the Environment's name, can be arbitrarily set, in fact, is a folder, in the home under the User name folder can be found.
Source Env/bin/activate #这样进进入了virtualenv的虚拟开发环境.
After entering the virtual environment, the command line will start to show (ENV), the Representative has entered the environment, then you can install Virtualenvwrapper and Django

Enter command line pip install Virtualenvwrapper
It is not possible to use sudo here, because in virtualenv, it is one of the most convenient designs without administrative privileges.

After the Virtualenvwrapper is installed, it will list the virtualenv as a dependent package, so it will be installed Automatically.

Open a new shell, enter mkvirtualenv test . If you open another shell, you are not in this virtualenv, you can workon test start by. If your work is done, you can use it deactivate to deactivate it.

Phi Blog



Create a virtual environment and go to useCreate a virtual environment with the system default Python version

Virtualenv--no-site-packages ubuntu_env

Under Linux:


Under Windows:


You can create an ENV directory (the virtual Environment name, which is the Python virtual Environment) in the current directory, and you will notice that virtualenv will install Python,setuptools and Pip on You.

Note: virtualenv is installed under py3, the VIRTUALENV command is only available if PY3 is the default Python version of the system, otherwise the command will not be Prompted.

Customizing the Python version to create a virtual environment 1. Install Python that requires a version
2. Specify the python version in virtualenv
virtualenv--no-site-packages--python=2.7 env

Note:

1. Before creating the virtualenv virtual environment, the corresponding version of Python must be installed on the system, and the current virtual environment will be invalid after Uninstallation. Both Python2 and Python3 can be present in the system, with the system variable path (not the user Variable) in the environment variable controlling the CMD or which version of Python is used in the system, which version of the path is preferred in the preceding Version.

2.–no-site-packages indicates that the system global Python installation package is not included, which makes the environment cleaner

2.–python=python2.7 specifies that the version of Python is not already installed by the system Python2.7

3. Env is the established virtual environment name

4. The executable python does notexist error occurs without installing python2.7 or using the command virtualenv--no-site-packages--python=python2.7 env

Enter the virtual environment and activate

Linux (mac os):

. Ubuntu_env/bin/activate

Pika:/media/pika/files/mine/python_workspace/nlp$d env
Going to/media/pika/files/mine/env
Pika:/media/pika/files/mine/env$. Ubuntu_env/bin/activate
(ubuntu_env) pika:/media/pika/files/mine/env$d NLP
Going TO/MEDIA/PIKA/FILES/MINE/PYTHON_WORKSPACE/NLP
(ubuntu_env) Pika:/media/pika/files/mine/python_workspace/nlp$python
Python 3.4.3 (default, Oct 14 2015, 20:28:29)

Windows:

env > Script\activate (or script\activate.bat)


MinGW (git):

Venv >source Scripts/activate


At this point, you will find that you have entered the virtual machine in front of the command line with an extra (env) Out. Now you can use pip install xxx to install the library you Want.

The Note:activate.bat script sets the console environment variable so that the Python command that the console calls later executes Python in the virtual Environment. After virtual env is activated, enter Python in any directory, and Ipython (if Installed) will invoke the command in virtual Env.

Exiting a virtual environment

(ubuntu_env) pika:/media/pika/files/mine/env/ubuntu_env$deactivate
pika:/media/pika/files/mine/env/ubuntu_env$

The note:deactivate command should be in the VIRTUALENV installation directory.

Use commands directly in this environment to deactivate exit


Note: Deactivate.bat is a restore console environment variable setting

Deleting a virtual Environment Rm-r venv

Directly delete the folder where the virtual environment is located venv deleted the VENV virtual environment we created

Linux under virtualenv other commands

List Virtual Environments

Workon

You can also use lsvirtualenv

Create a new virtual environment

MKVIRTUALENV [virtual Environment name]

Start/switch Virtual Environments

Workon [virtual Environment name]

Deleting a virtual environment

RMVIRTUALENV [virtual Environment name]

Phi Blog



Virtual Environment Migration

When you need to transfer the virtual environment to another directory on the same computer

Or when you need to migrate or replicate a virtual environment ENV1 to another virtual environment (perhaps not on the same machine) env2, you will first need to install PIP and virtualenv on the destination machine, and then install the other package in one of the following ways:

1. Copy all the files in the ENV1 directly into the env2, and then modify the files involved in the Path. This method may work normally, but it is obviously not a good idea (though the LZ is the one used when the network is bad).

The modified path is mainly the path of the file execution program under the Ubuntu_env/bin directory of the virtual environment: such as active, pip, Ipython and so on (almost all file paths, But some commands you do not need to change).

If you modify the path in the virtual environment ubuntu_env/bin/active:

In fact, the virtual_env path is best modified to the current absolute path virtual_env= "/media/pika/files/mine/env/ubuntu_env";

Modify the paths in the virtual environment Ubuntu_env/bin/pip and PIP3 and pip3.4 to absolute paths: #!/home/pipi/env/ubuntu_env/bin/python3

Note It is best not to modify the path to a relative path. If the modified Ipython execution path is relative to path #!. /bin/python3, which is relative to this ipython.py execution path, after activating virtual env, If you enter Python in another directory, it will be wrong to find or directly invoke Python in the system, not Python in virtual env , and the original default path uses an absolute path to be able to execute Ipython without error in any directory after virtual env is activated!


2. Using Requirements.txt

Enter the original virtual environment env1, and then execute PIP freeze > Requirements.txt to save the package dependency information in the Requirements.txt File.

It is best to manually adjust the order, such as NumPy and scipy to be installed in front of matplotlib, in addition, if you want to install the latest version, then the later version number ==1.9.1 what to Delete.

Then enter the destination virtual environment env2, execute PIP install-r requirements.txt,pip will automatically download and install all the packages from the Internet.

Virtual Environment Env2 If it is a copy of env1, it is best to first pip Uninstall-ry requirements.txt, then pip Install-r requirements.txt


3.pip By default downloads the package installation file from the PyPI server (http://pypi.python.org/simple), if the destination machine is unable to connect to the network, you can use the following methods:
3.1 Build Your own PyPI server. professional, You can use a Third-party software package to build a complete pypi mirror server, refer to Http://www.worldhello.net/2011/03/14/2357.html. Faster method only need a command python-m Simplehttpserver can complete the building server, the specific directory structure can refer to the original PyPI server, in short, is to pack the installation files into the Directory. After setting up the server, in the destination virtual environment, you can use PIP to install, command such as: pip Install-i http://127.0.0.1:8000/-r requirements.txt
3.2 If you really do not want to build PyPI server, there are ways. Download the installation files for all packages first, you can download them manually, or you can use pip, such as Pip Install-d/path/to/save/-r requirements.txt, then modify the Requirements.txt file yourself, and change each line to the path of the corresponding package installation File. finally, use PIP installation in the destination virtual environment, such as Pip Install-r Requirements.txt.

3.3 There is another way, the bundle option that PIP Provides. First execute PIP bundle myenv.pybundle-r requirements.txt, a Myenv.pybundle file is generated that contains the installation files for all packages (note that the suffix must be. pybundle), the default is to re-download the installation file from the PyPI server, or, if you prefer, to use the method in 3.1 to specify the local py Pi Server. Then execute pip install Myenv.pybundle in the destination virtual Environment.

so, LZ prefers the python development environment that is configured in Docker.

Virtual Environment Migration Error

Method 1 packet Ingestion error after virtual environment migration

From scipy import stats

Importerror:liblapack.so.3:cannot Open Shared Object

At this point only need pip install scipy reinstall again OK.

Importerror:libblt.2.4.so.8.6:cannot open Shared object file:no such file or directory, please install the PYTHON3-TK p Ackage

Also reinstall Matplotlib

So it's best to use Method 2 Migration.

Phi Blog



Install Python expansion pack in virtualenvPip installation, using the command pip, pip3, pip3.4 can be, because virtualenv installed in if Py3 then pip corresponds to pip3, in the directory virtual environment ubuntu_env/bin/ There are three commands Pip and PIP3 and pip3.4 will Know.
Also note that the installation of Python expansion packs in a virtual environment needs to go into the virtual environment first, or it will be installed in the Python path of the System.virtualenv Installing the Python library from the Requirements.txt

Enter the original virtual environment env1, and then execute PIP freeze > Requirements.txt to save the package dependency information in the Requirements.txt File. Then enter the destination virtual environment env2, execute PIP install-r requirements.txt,pip will automatically download and install all the packages from the Internet.

Requirements.txt:

NumPy

Pandas

installing NumPy in virtualenv

Install numpy! via pip install numpy (it may be that the LZ system already exists in the NumPy Non-python dependent ... )

installing scipy and matplotlib in Virtualenv

You can have the to install some extra Non-python dependencies using Apt-get. before installing scipy, install scipy Non-Python dependencies in the system, as if it were fortran.

Building matplotlib requires libpng (and freetype, as Well) which isn ' t a python library, so pipdoesn ' t handle installing It (or freetype).

sudo apt-get build-dep python-scipy

sudo apt-get-y build-dep matplotlib

(virtualenv) pip Install scipy
(virtualenv) pip Install matplotlib

[Install python-scipy in a virtualenv]

[how to install matplotlib using virtualenv on Ubuntu]

[installing]

install Pywin32 in Windows Virtualenv

Virtualenv can not be used directly with Pip install pywin32, or error:Could not find any downloads that satisfy the  Requirement. Pywin32 No distributions at all found for pywin32.

Solution1

Find the EXE file you want in http://sourceforge.net/projects/pywin32/files/

Activating virtualenv

Run Easy_install Http://PATH.TO/EXE/DOWNLOAD

Note: Easy_install may be wrong if it is too old: error:c:\users\blah\appdata\local\temp\easy_install-ibkzv7\ Pywin32-214.win32-py2.6.exe is not a valid distutils Windows. exe

Solution2

You can do this if you can't download it.

Http://sourceforge.net/projects/pywin32/files/download exe file to local

Activating virtualenv

Run Easy_install Downloaded_file.exe e.g. Easy_install D:\Downloads\Programming\Python\python3\ Pywin32-219.win-amd64-py3.4.exe

Solution3 (pro-test Effective)

After installing Pywin32 in the system, copy the pywin32.pth to virtualenv site-packages and edit the file to specify the relevant path. [PyWin32 and virtualenv]

> Copy D:\python3.4.2\Lib\site-packages\pywin32.pth E:\mine\python_workspace\CrawlerEnv\Lib\site-packages\ Pywin32.pth

Investigate the contents of the document more E:\mine\python_workspace\CrawlerEnv\Lib\site-packages\pywin32.pth '

#. PTH file for the PYWIN32 extensions
Win32
Win32\lib
Pythonwin

Change it from:

D:\python3.4.2\Lib\site-packages\win32
D:\python3.4.2\Lib\site-packages\win32\lib
D:\python3.4.2\Lib\site-packages\Pythonwin

This can be successfully introduced and can be used in pycharm:


[how to install win32com module in a virtualenv?]

[how can I use Pywin32 with a virtualenv without have to include the host environment ' s site-packages folder?]

[how to install pywin32-virtualenv with Python 2.7-bit?]

installing rsae in virtualenv

Python version is Python3


Note: LZ do not know why to restart the computer first to install success, or error could not find the version of that satisfies the requirement RSA

View a list of Python packages already installed in Virtualenv

(ubuntu_env) pika:/media/pika/files/mine/python_workspace/ubuntu_env$pip list
NumPy (1.10.4)
Pip (8.1.1)
Py4j (0.9.2)
Setuptools (20.3.1)
SH (1.11)
Wheel (0.29.0)
(ubuntu_env) pika:/media/pika/files/mine/python_workspace/ubuntu_env$pip Freeze
numpy==1.10.4
py4j==0.9.2
scipy==0.17.0
sh==1.11

Note: The PIP in virtualenv should be PIP3 Default. There are two different ways to have a ding.

Phi Blog



pycharm Project configuration Virtual EnvironmentSetting up a project in Pycharm run in virtualenv

First virtualenv venv Create a virtual environment and install the relevant operating Environment.

Open Pycharm


You can select the desired operating environment in the Drop-down box

If you cannot find the virtual environment that you created, you can add a new Python interpreter to the right-hand settings button, and add local to select the Python interpreter in the virtual environment venv that you created, such as D:\venv\Scripts\ Python.exe) It's Ready.

Phi Blog



multiple different Python versions coexist

There are many ways we can make different versions of Python coexist on the system, such as OS X, if installed with the official DMG version, the Python2 and newly installed Python3 can Coexist. Python3 can be invoked using Python3 , and even pip can be invoked using PIP3 .

1. Configure yourself

Install Python2 and Python3 at the same time, and then modify the order of two in the system variables to set which version of Python to use in the system First.

If you install Python2 with anaconda, then install python3, you must set the PIP in Python3 setup, or the installer will fail to install successfully (so it is always necessary to install the extension package. Never use Anaconda to install so many useless things at once). After installing Python3 successfully, download and run the get-pip.py file installation pip will be Ok.

2. pyenv

If there are many small versions that need to coexist, pyenv is used to solve such problems. It can install, uninstall, compile, Manage multiple versions of python, and set one of them as a working environment at any Time. But Pyenv does not support Windows Systems. [python multi-version Coexistence of pyenv]

3. Pywin

There is a pyenv alternative to Windows that is Pywin. It is used to switch between multiple installed Python versions (that is, Multiple versions of Python are already installed in windowns), and Msys/mingw32 is Supported.

Installation and Use:

Pip Install Pywin

c:\>Pywin genlaunchers
Search for Python installations and create batch files in the same directory where Pywin is Installed.

c:\>Pywin # Launch first python found in PATH
c:\>pywin-2.7 # launch Python 2.7
c:\>Pywin setdefault 3.4
Setting default Python for active session to:3.4
C:\Python34; C:\Python34\Scripts-now at front of PATH #相当于1中的自己配置

c:\>pywin-3.2 test.py # launch test.py with Python 3.2
C:\Python32


Pywin the order in which Python versions are started:
Any version specified after a #! In the first 2 lines of the Source. The interpreter is invoked with any additional parameters.examples:
#! python3.3
#! /usr/bin/python2.7-v
IF the environment variable virtual_env is set, use that virtualenv ' s Python.exe.
The IF the environment variable pythonhome is set with the use of its python.exe.
If none of the above, fall back to the first python.exe found on the path.
4. Python Launcher for Windows

Python starts with the 3.3 version (again 3.3?). ), comes with a py.exe boot tool in the Windows System. If you are installing a Python 3.3 (or later) Environment using the installation package downloaded from the python.org website, you can use this tool directly at the command Prompt. py can open the default Python prompt, and py-2.7 and py-3 open the corresponding Python Version.

[Python Virtual environment]

[coexistence of multiple Python versions under windows]

Phi Blog



Another solution Anaconda

Download Anaconda

~/env$ wget http://repo.continuum.io/archive/Anaconda3-4.1.1-Linux-x86_64.sh

Python 3:

Bash anaconda3-4.1.1-linux-x86_64.sh

When running the installation note do not always enter, to set the Anaconda installation directory and whether to join the user Default. bashrc.

This installs the Python is the user independent! Linux different user input python is the corresponding installation of Anaconda,pip Also.

from:http://blog.csdn.net/pipisorry/article/details/47008981

Ref: using Vitrualenvwrapper to isolate a Python project's library dependencies

A new way to deploy Python code DH-VIRTUALENV


Python Virtual Environment virtualenv installation and configuration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.