windows python Virtual Environment virtualenv Installation and Use Detailed Explanation

  • 2021-07-18 08:31:19
  • OfStack

The installation of python in ubuntu16.04 environment and python virtual environment virtualenv is introduced above, and the installation and use in windows environment are introduced below.

Environmental information

Operating system: windows10 python version: 3.6. 3

Installation

1. Download and install python 3.6. 3

Enter official website: https://www.python.org/downloads (windows)

Selection: Windows x86-64 web-based installer


Windows x86-64 web-based installer #  It refers to the need for networking when installing, which is small in size and fast to download. It is recommended 
Windows x86-64 executable installer #  Refers to the use of XXX.exe Files are installed in the way, and the download volume is large 
Windows x86-64 embeddable zip file #  Embedded version 

1. Double-click to start installation, pay attention to checking add python to path, and other 1-way defaults;

2. Test: Enter python on the command line and display python interactive interface to install successfully;

Attention

If you don't recognize python, the most likely reason is that adding environment variables is unsuccessful. Go to the system environment variables to see 1, and add it if you don't;

Installing virtualenv

python version 3.6. 3 comes with pip. In order to reduce installation steps, pip is used for installation.

cmd, open the windows command line;


pip install virtualenv

pip install virtualenvwrapper #  This is right virtualenv The encapsulated version of, 1 It must be in virtualenv Rear installation  

After installation, enter pip list to view all currently installed packages;

Create a virtual environment

Select a file to store the virtual environment, such as E:/python3


cd E:python3 #  Enter the file 
virtualenv envname  #  Create 1 The name is envname Virtual environment based on 
dir   #  Look at the current directory to know 1 A envname The file of has been created 

virtualenv -p python2 envname #  If you install more than one python Versions, such as py2 And py3 You need to specify which one to use to create the virtual environment 

Note:

If the virtualenv command is not recognized, it may be that the python installation path is not added to the system environment variable, or virtualenv is not installed or an cmd window is not reopened;

Start the virtual environment


#  Enter the virtual environment file 
cd envname
#  Enter the relevant startup folder 
cd Scripts

activate #  Start the virtual environment 
deactivate #  Exit the virtual environment 

Below, you can freely install various packages in the virtual environment.

Problems and details

Every time you need to start the virtual environment, you need to enter the directory of Scripy in the folder of the virtual environment, which is very inconvenient. You can add the path of Scripy to the system environment variable.

If other python versions are installed on the system, such as python 2.7. 13; After adding its path to the system environment variable, enter the installation file directory, and change the python. exe file into python2.exe file to prevent command conflict;

If there are multiple versions of python, then there are multiple versions of pip, which requires the python version to be specified for pip installation.


python -m pip install xxx  # python3 Version installation package 
python2 -m pip install xxx # python2 Version installation package 

After adding system environment variables, you need to close the current cmd window and restart 1 window before it will take effect.


Related articles: