Install Virtualenv in Windows to create an independent Python Environment

Source: Internet
Author: User
Tags virtualenv
Sometimes different versions of Python packages and modules need to be called in our program, so using Virtualenv's virtual environment can help us isolate and use them, next, let's take a look at how to install Virtualenv in Windows to create an independent Python Environment. sometimes our program needs to call different versions of Python packages and modules, using Virtualenv's virtual environment can help us isolate usage. Next, let's take a look at how to install and use Virtualenv in Windows to create an independent Python environment.

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

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.

For more articles about installing Virtualenv in Windows to create an independent Python environment, refer to PHP Chinese network!

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.