Windows installation virtualenv to create a standalone Python environment

Source: Internet
Author: User
Tags virtual environment virtualenv
Sometimes we need to invoke different versions of Python packages and modules in our program, so we can use Virtualenv's virtual environment to help us isolate them, so let's take a look at installing Windows with virtualenv to create a standalone Python environment

0. When will the virtualenv be used?
Suppose that the two applications in the system, where a application to the library Libfoo version requirements of 1, and B application to the same library Libfoo version requirements of 2, two applications to the same library requirements to conflict, and the system in the library by default installed to/usr/lib/python2.7/ Site-packages, how can you use both applications at the same time?
You do not have enough user rights to install the package into the Site-packages directory
Need to use one application and be independent of other applications in the system.
If you encounter the above problems, then virtualenv will help you. Virtualenv can create an environment that has its own installation directory and does not interfere with the system's shared libraries or other virtualenv environments.

1, installation Virtualenv
Before installing virtualenv, we need to install at least one version of Python, because Virtualenv is a third-party module of Python and must be installed based on the Python environment;
If your Python environment has a PIP, then use the command directly:

Pip Install Virtualenv

Install it, otherwise you need to download the source code, and then use the command: Python install setup.py
After installation, you will need to create a new virtualenv independent environment to view its Help commands:

Virtualenv-h

The commonly used parameters are:
-P: Specifies a version of the Python environment, which is typically used when multiple Python versions are installed on your system, and by default virtualenv takes precedence over its hosting Python environment. That is, it installs under that Python version and chooses which version will default to the default Python isolation environment.
--no-site-packages: A Python installation package that does not use the system environment, that is, an installation package in which the real Python environment cannot be used in the quarantine package; The current version is the default.
--system-site-packages: A Python installation package that, contrary to the above, enables the isolated environment to access the system environment
--distribute:copy a branch of a Python environment that installs basic modules such as Setup, Pip, wheel, etc. by default

2. Activation
Once the installation is finished, you can create an isolated environment using the command:

Virtualenv Envname # #创建一个新的隔离环境 CD envname scripts\activate  # #激活并切换到virtualenv环境

3. Use
After creating and activating the VIRTUALENV environment, we can use and install Python installation packages and modules in a normal way. You can also switch directly to the Python file directory that needs to be executed, using Python xxx.py to ensure that Python files are executed in an isolated environment.
That is: Before we need to use the VIRTUALENV environment, just activate and then use the same way as before to execute the python operation, run the Python file

CD my_project_foldervirtual Envsource bin/activatedeactivate

The above is the basic use of virtualenv process. First, virtual env creates a virtualized environment named Env in the current directory, where three directories are automatically generated in the current directory: Bin, include, and Lib. The virtual environment created by default will depend on the site-packages in the system, that is, the installed libraries in the system are still available in the virtual environment, and if you want to remove dependencies, you can:

Virtualenv--no-site-packages ENV

。 The source bin/activate enables you to launch a virtual environment under the current directory. When started, the Shell's prompt will change, indicating that it has entered the virtual environment. Use deactivate to leave the virtual environment.


4. Integration into the development environment
Currently in Pycharm to support the integration of VIRTUALENV environment, the other IDE is temporarily not researched, integration is also very convenient:
(1) Create a new project
(2) Click on the Interpreter Bar settings icon
(3) Select Virtualenv
(4) can be new or can choose an existing virtualenv environment

After the configuration we can also test in the project, select a difference with the system environment of the installation module to see if its version is correct, I here because there are 2 Django version, so use the Django module to test, get the correct results.


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.