Using virtual environments under Windows Python

Source: Internet
Author: User

Official documents:
Http://pythonguidecn.readthedocs.io/zh/latest/dev/virtualenvs.html

Virtualenv is a tool for creating an isolated python environment. VIRTUALENV Create a folder that contains all the necessary executables to use the packages required by the Python project.

It can be used independently, instead of pipenv.

Install virtualenv with PIP:

$ pip install virtualenv
    • 1

To test your installation

$ virtualenv --version
    • 1
1. Basic use

Create a virtual environment for a project:

$ cd my_project_folder$ virtualenv my_project
    • 1
    • 2

Virtualenv My_project will create a folder in the current directory that contains the Python executable file and a copy of the PIP library so that the other packages can be installed. The name of the virtual environment (in this case, my_project) can be arbitrary, and omitting the name will place the file in the current directory.

In any directory where you run the command, this creates a copy of Python and puts it in a file called My_project.

You can choose to use a Python interpreter (for example python2.7 ):

$ virtualenv -p D/python27/python2.7 my_project
    • 1

To get started with a virtual environment, it needs to be activated:

$ my_project/Scripts/activate
    • 1

The name of the current virtual environment will be displayed to the left of the prompt (for example (My_project) your computer: your project username $) to let you know that it is active. From now on, any package you install with PIP will be placed in the My_project folder, isolated from the globally installed Python.

Install packages as usual, such as:

$ pip install requests
    • 1

If you have temporarily completed your work in a virtual environment, you can deactivate it:

$ my_project\Scripts\deactivate
    • 1

This will return to the system's default Python interpreter, which will also return to the default, including the installed libraries.

To delete a virtual environment, simply delete its folder. (To do so please perform RM-RF my_project)

Then after a while, you may have many virtual environments scattered throughout the system, and you will probably forget their names or locations.

Virtualenv Common Command Options

virtualenv [OPTIONS] Dest_dir

- -
–version Displays version information.
-h,–help Displays help information.
-v,–verbose Increase the background output information.
-q,–quiet Control the background output information.
-P Python_exe,–python=python_exe Specifying the Python Interpreter
–clear Clears dependent libraries in the virtual environment and initializes the environment.
–system-site-packages Uses the library that is already installed on the current Python body.
–always-copy Do not use symbolic links to copy files directly.
–no-setuptools Don't install Setuptools in the new virtualenv.
–no-pip Don't install PIP in the new virtualenv.
–no-wheel Don't install wheel in the new virtualenv.
2. Other Notes

Running virtualenv with the –no-site-packages option will not include a globally installed package. This can be used to keep the package list clean in case you need to access it later. (This is the default behavior in Virtualenv 1.7 and beyond)

In order to maintain the consistency of your environment, the current state of the "Freeze (freeze)" Environment package is a good idea. To do this, please run:

$ pip freeze > requirements.txt
    • 1

This will create a requirements.txt file that contains a simple list of all the packages and their respective versions in the current environment. You can use the PIP list to view the list of installed packages without generating a requirements file. This will make it easier for a different developer (or you, if you need to recreate such an environment) to install the same version of the same package later.

$ pip install -r requirements.txt
    • 1

This helps to ensure consistency between installation, deployment, and developers.

Finally, remember to exclude the virtual Environment folder from source version control and add it to the list of ignore. (view version control ignored)

3. Virtualenvwrapper

Virtualenvwrapper is a virtualenv extension package that manages virtualenv easily. Provides a series of commands that make working with virtual environments much more enjoyable. It puts all your virtual environments in one place.

Install (make sure the virtualenv is installed):

$ pip install virtualenvwrapper-win
    • 1
Basic use

Create a virtual environment:

$ mkvirtualenv my_project
    • 1

Switch to a virtual environment

$ workon my_project
    • 1

Alternatively, you can create a project that creates a virtual environment and creates a project directory in the $WORKON _home. When you use Workon MyProject, you cd-ed to the project directory.

$ mkproject myproject
    • 1

Virtualenvwrapper provides the tab completion function for the environment name. This is useful when you have many environments and it is difficult to remember their names.

Workon can also stop your current environment, so you can quickly switch between environments.

The stop is the same:

$ deactivate
    • 1

Delete:

$ rmvirtualenv my_project
    • 1
Other useful commands
lsvirtualenv
    • 1

List all the environments.

cdvirtualenv
    • 1

Navigate to the directory of the currently active virtual environment, for example, so you can browse its site-packages.

cdsitepackages
    • 1

Similar to the above, but directly into the Site-packages directory.

lssitepackages
    • 1

Displays the contents of the Site-packages directory.

Using virtual environments under Windows Python

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.