This is the 24th day of my participation in the August Text Challenge.More challenges in August

Is it necessary to configure virtual environments?

Virtual environments are a tool that places the dependencies required by different projects in separate locations, creating virtual Python environments for those projects. It solves the “project X depends on version 1.x, while project Y needs project 4.x” dilemma and keeps your global site-packages directory clean and manageable. Virtualenv is a tool to create an isolated Python environment. Virtualenv creates a folder containing all the necessary executables to use the packages required for Python projects.

How do I install a virtual environment in different operating systems?

The operating system versions used in this article are Windows 7 64-bit and Ubuntu 16.04

Install the virtual environment using virtualEnv

windows7 64bit

Make sure you have Python and PIP installed. If you don’t, look at search engines.

Installation:

pip install virtualenv
Or use Douban source to install faster
pip install -i https://pypi.douban.com/simple/ virtualenv
Copy the code

Create a virtual environment:

You can use virtualenv [virtual environment name] to create a virtual environment. The operation screenshot is as follows:

Can be used when multiple Python versions exist in the local environmentvirtualenv -p [.../python.exe]To create the specified version of the virtual environment.

Enter the virtual environment:

Run the CD command to switch to the script directory in the created virtual environment folder and run activate.bat to go to the virtual environment. The operation is as follows:

You can see that the command line begins with (virtual environment name), which indicates successful entry into the virtual environment.

Exit the virtual environment:

Run the CD command to switch to the script directory in the created virtual environment folder, run deactivate.bat to go to the virtual environment, and run deactivate.bat to exit the virtual environment.

Delete a virtual environment:

Delete the corresponding folder.

Ubuntu 16.04

Make sure you have Python and PIP installed. If you don’t, look at search engines.

# installation PIP
sudo apt-get install python3-pip
Python2.7 is built into Ubuntu, so we can set Python3 as the default or not
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150
# Switch back
sudo update-alternatives --config python
Select a number and press Enter
Copy the code

Installation:

pip install virtualenv
Copy the code

Create a virtual environment:

Virtualenv [virtual environment name]

Create a virtual environment using the specified version of Python
virtualenv -p /usr/local/bin/python3 [virtual environment name]Copy the code

Enter the virtual environment:

Switch to the virtual environment directory, switch to… In /bin, run the following command.

source activate.sh
Copy the code

Exit the virtual environment:

deactivate
Copy the code

Deleting a Virtual Environment

Rm -rf [Virtual environment name]Copy the code

Install the virtual environment using virtualenvWrapper

If you’ve followed this article up to this point, you’ll find virtualEnv very difficult to manage, so it’s recommended to use virtualenvWrapper directly to create a managed virtual environment.

windows7 64bit

Installation:

pip install virtualenv-win
Copy the code

Create a virtual environment:

Configure an environment variable first, so that the virtual environment created by default will be created under the environment variable, as shown in the following figure:

You can use mkvirtualenv [virtual environment name] to create a virtual environment. The operation screenshot is as follows:

Install the virtual environment using the specified version of Python:

Mkvirtualenv --python=[python installation directory /python.exe]Copy the code

List all existing virtual environments:

Entering and exiting the virtual environment:

Run the workon [virtual environment name] command to enter the virtual environment and run the deactivate command to exit the virtual environment.

ubuntu16.04

Installation:

pip install virtualenvwrapper
Copy the code

Configuration:

sudo vim ~/.bashrc
export WORKON_HOME=$HOME/.virtualenvs  All virtual environments store directories
source /usr/local/bin/virtualenvwrapper.sh
source ~/.bashrc
Copy the code

Create a virtual environment:

mkvirtualenv env_name # env_name specifies the name of the virtual environment you want to create
Copy the code

Enter the virtual environment:

Workon env_name workon + TAB twice displays all virtual environmentsCopy the code

Exit the virtual environment:

deactivate
Copy the code

Delete a virtual environment:

rmvirtualenv env_name
Copy the code

An error occurs when installing virtualenvwrapper in Ubuntu

Source ~/.bashrc error:

/usr/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.
Copy the code

Solution:

Add a line of configuration to the.bashrc file specifying the Python path
export WORKON_HOME=$HOME/.virtualenvs  
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 # This time added
source /usr/local/bin/virtualenvwrapper.sh
Copy the code

Virtualenvwrapper installation error:

Couldn't find index page for 'pbr' (maybe misspelled?) Download error on https://pypi.python.org/simple/: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645) -- Some packages may not be found! No local packages or download links found for pbr Traceback (most recent call last): File "", line 1, in File "/tmp/pip-build-6hblrn57/virtualenvwrapper/setup.py", line 7, in pbr=True, The File "/ usr/lib/python3.5 distutils/core. Py", line 108, in setup _setup_distribution = dist = klass(attrs) File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 269, in __init__ self.fetch_build_eggs(attrs['setup_requires'])
File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 313, in fetch_build_eggs
replace_conflicting=True,
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 826, in resolve
dist = best[req.key] = env.best_match(req, ws, installer)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 1092, in best_match
return self.obtain(req, installer)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 1104, in obtain
return installer(requirement)
File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 380, in fetch_build_egg
return cmd.easy_install(req)
File "/usr/lib/python3/dist-packages/setuptools/command/easy_install.py", line 657, in easy_install
raise DistutilsError(msg)
distutils.errors.DistutilsError: Could not find suitable distribution for Requirement.parse('pbr')
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-6hblrn57/virtualenvwrapper/
Copy the code

Solution:

sudo pip install-i https://pypi.douban.tsinghua.edu.cn/simple pbr
sudo pip install-i https://pypi.douban.tsinghua.edu.cn/simple--no-deps stevedore
sudo pip install-i https://pypi.douban.tsinghua.edu.cn/simple--no-deps virtualenvwrapper
Copy the code