Install and use Virtualenv in Windows to create an independent Python environment, virtualenvpython

Source: Internet
Author: User
Tags virtualenv

Install and use Virtualenv in Windows to create an independent Python environment, virtualenvpython

0. When will virtualenv be used?
Assume that two applications in the system, where application A requires 1 for the LibFoo version of the library, and application B requires 2 for the LibFoo version of the same library, the requirements of the two applications for the same library conflict, and the libraries in the system are installed in/usr/lib/python2.7/site-packages by default. How can these two applications be used at the same time?
Your user permissions are insufficient, so you cannot install the package to the site-packages directory.
One application is required and independent from other applications in the system.
If you encounter the above problems, virtualenv will help you. Virtualenv can create an environment with its own installation directory without interfering with the shared libraries of the system or other virtualenv environments.

1. Install virtualenv
Before installing virtualenv, We need to install at least one version of Python. Because virtualenv is a third-party module of python, it must be installed in the python environment;
If your python environment has pip, use the following command:

pip install virtualenv 
Install it. Otherwise, you need to download the source code and run the command: python install setup. py to install it.
After the installation, you need to create an independent environment for virtualenv. For details, refer to the help command:
virtualenv -h

Common parameters include:
-P: Specifies a version of the python environment. It is usually used when multiple python versions are installed in your system. By default, virtualenv preferentially selects its host python environment, that is, if it is installed in that python version, the default version will be used as the default python isolation environment.
-- No-site-packages: the python installation package in the system environment is not used. That is, the installation package in the real python environment cannot be used in the isolation package. The current version is the default one.
-- System-site-packages: opposite to the above, enabling the isolated environment to access the python installation package of the system Environment
-- Distribute: copy the branch of a python environment. By default, basic modules such as setup, pip, and wheel are installed.

2. Activation
After the installation is complete, you can create an isolated environment by running the following command:

Virtualenv envname # create a new isolation environment cd envname Scripts \ activate # activate and switch to the virtualenv Environment
3. Use
After creating and activating the virtualenv environment, we can use and install the python installation package and module in a normal way. You can also directly switch to the python file directory to be executed, and use python xxx. py to ensure that python files are executed in an isolated environment.
That is: Before we need to use the virtualenv environment, we only need to activate it and then use the same method to execute python operations and run python files.

cd my_project_foldervirtual ENVsource bin/activatedeactivate

The above is the basic use process of virtualenv. First, virtual ENV creates a virtual environment named ENV in the current directory. At this time, three directories are automatically generated in the current directory: bin, include, and lib. By default, the created virtual environment depends on the site-packages in the system. That is, the installed libraries in the system can still be used in the virtual environment. To remove the dependency, you can:

virtualenv --no-site-packages ENV
. You can use source bin/activate to start the virtual environment in the current directory. After the shell is started, the shell prompt changes, indicating that the virtual environment is entered. Use deactivate to exit the virtual environment.


4. Integration into the development environment
Currently, PyCharm supports virtualenv environment integration. Other ides have not been studied yet. Integration is also very convenient:
(1) create a project
(2) Click the setting icon in the interpreter bar.
(3) Select virtualenv
(4) You can create or select an existing virtualenv environment.

After configuration, we can also test it in this project, select an installation module for the difference and system environment, and check whether the version is correct. I have two django versions here, therefore, the django module is used for testing and the correct result is obtained.

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.