SoFunction
Updated on 2024-11-15

Steps to install Python virtual environments on win10 and linux respectively

Many beginners will use windows as a development machine, today we will see how to install Python virtual machine environment in win10 and Linux respectively. Virtual environments have a lot of advantages, today we use the virtual environment is virtualenv.

virtualenv is used to create independent Python environments, where multiple Pythons are independent of each other and do not affect each other, and it can:

1. Installation of new packages without permissions

2. Different applications can use different versions of the suite

3. Package upgrades do not affect other applications

Installation under win10

1. Open cmd to install the virtual environment package

pip install virtualenvwrapper-win

2. Configuring environment variables

WORKON_HOME

E:\Python_Envs

3. Open a new cmd window and install the virtual environment.

Create two pure virtual environments

mkvirtualenv --python=C:\SoftWare\Python27\ python27
deactivate
mkvirtualenv --python=C:\SoftWare\Python36\ python36
deactivate

Establishment of a virtual environment for use

mkvirtualenv --python=C:\SoftWare\Python27\ py27
deactivate
mkvirtualenv --python=C:\SoftWare\Python36\ py36
deactivate

Installation of common packages (Python36)

workon py36
pip install PIL
pip install pymysql
pip install django
deactivate

Command Introduction

mkvitualenv Env create virtual environment --python can specify python version

workon Viewing Virtual Environments

workon Env Enter the virtual environment

pip list View the list of installed packages --format=columns

Specify the display mode

deactivate Exits the virtual environment

Installation under Linux

mounting

sudo apt-get install python-virtualenv

Usage

virtualenv [name of virtual environment]

E.g., create a **ENV** virtual environment

virtualenv ENV

By default, the virtual environment will depend on the site packages in the system environment, that is to say, third-party packages already installed in the system will also be installed in the virtual environment, if you do not want to depend on these packages, then you can add the parameter --no-site-packages to create a virtual environment

virtualenv --no-site-packages [Virtual Environment Name]

Starting a virtual environment

cd ENV
source ./bin/activate

Note that at this point the command line will have one more (ENV), ENV is the name of the virtual environment, and all the next modules will only be installed into that directory.

Exiting the virtual environment

deactivate

Installing the Python Suite in a Virtual Environment

Virtualenv comes with a pip installer, so packages that need to be installed can be run directly:

pip install [Kit Name]

If the virtual environment is not started and the pip tool is installed on the system, the suite will be installed on the system environment, to avoid this matter, you can add it to the ~/.bashrc file:

export PIP_REQUIRE_VIRTUALENV=true

Or let the system automatically turn on the virtual environment when pip is executed:

export PIP_RESPECT_VIRTUALENV=true

Virtualenvwrapper

Virtaulenvwrapper is an extension package for virtualenv for easier management of virtual environments, and it can do:

1. Consolidate all virtual environments under one directory

2. Managing (adding, deleting, copying) virtual environments

3. Switching virtual environments

4. ...

mounting

sudo easy_install virtualenvwrapper

At this point you can not use virtualenvwrapper, the default virtualenvwrapper installed in /usr/local/bin below, in fact, you need to run the file to be able to, do not rush, open this file to see, there are installation steps, we follow the operation of the environment to set up a good.

Create a directory to store the virtual environment

mkdir $HOME/.virtualenvs

Add lines to ~/.bashrc:

export WORKON_HOME=$HOME/.virtualenvs

Add lines to ~/.bashrc:

source /usr/local/bin/

Running:

 source ~/.bashrc

At this point virtualenvwrapper is ready to use.

This is the whole content of this article, I hope it will help you to learn more.