SoFunction
Updated on 2024-11-15

A brief introduction to the Python virtual environment and how to use it

I. Why do you need a virtual environment?

The environment, in this case, refers to the environment in which the Python code will run. It should contain the following information:

  • Python interpreter, which interpreter is used to execute the code?
  • Python library location, where to goimport Required modules?
  • The location of the executable program, e.g. the installation of thepipSo.pip Where are the orders?

Since each project may be different, for example, this project uses vtk 7.1, another project uses vtk 9.0, if you do not isolate the environment but install it globally, it will lead to package conflicts and thus problems, so this time to let each project have a set of independent Python environment, so that there will not be conflicts. Python virtual environments are designed to solve this problem. In short, a virtual environment is a copy of the system Python environment.

Common tools for Python virtual environments are the following:

  • virtualenv
  • venv
  • pipenv

II. virtualenv

virtualenv is the most popular tool for configuring Python virtual environments. Not only does it support both Python2 and Python3, but you can specify the Python interpreter for each virtual environment and choose not to inherit packages from the base version.

Benefits of virtualenv

  • Make development environments for different Python applications independent of each other
  • Upgrading the development environment does not affect the development environments of other applications, nor does it affect the global environment (the default development environment is the global development environment), because the virtual environment is a private copy of the global environment, and when I pip install in the virtual environment, it will only be installed in the selected virtual environment.
  • It prevents package management confusion and version conflicts in the system

1. pip install virtualenv

pip install virtualenv 

2. Creating a virtual environment directory

mkdir myproject
cd myproject

3. Create a standalone Python runtime environment: myenv

virtualenv --no-site-packages myenv
# --no-site-packages The significance of this is that it doesn't copy what is already installed on the systemPythonenvironment to get all the third-party packages in a“unmixed”operating environment。

At this point, a directory named "myenv" will be created in the current directory, which will contain the virtual environment files you just created.

4. Activation of the virtual operating environment

# Windows
myenv\Scripts\
 
# Linux
source myenv/bin/activate

After executing the above commands, you will find that the command prompt has changed, and there is a (myenv) prefix, which means that the current environment is a Python environment named "myenv", and you can install libraries, run programs, etc., in this environment in a familiar way.

5. Use the deactivate command to exit the current myenv environment.

deactivate

III. venv

Since version 3.3, Python comes with a virtual environment, venv, which is similar to virtualenv in many ways, but has a different mechanism. Because it comes with version 3.3, this tool only supports python 3.3 and later. So, to use a virtual environment on python2, you still need to use virtualenv.

1. pip install venv

# Windows
windowscentervenvAlready exists in the form of a standard library,No need for a separate installation
 
# Linux
sudo apt install python3-venv  # If there are different versions ofPython3,Specific versions can be specifiedvenv:python3.5-venv

2. Create a standalone Python environment in the current directory: myenv

# Windows
py -3 -m venv myenv  
 
# Linux 
python3 -m venv myenv

3. Activation of the virtual operating environment

# Windows
myenv\Scripts\
 
# Linux
source myenv/bin/activate

After executing the above commands, you will find that the command prompt has changed, and there is a (myenv) prefix, which means that the current environment is a Python environment named "myenv", and you can install libraries, run programs, etc., in this environment in a familiar way.

4. Use the deactivate command to exit the current myenv environment.

deactivate

IV. pipenv

pipenv is a command-line tool written by Kenneth Reitz, the main advocate of Pipfile and author of requests, which mainly contains pipfile, pip, click, requests and virtualenv, and is able to effectively manage multiple environments, various third-party packages and modules for Python.

The main features of pipenv:

  • pipenv integrates the functions of pip and virtualenv, and improves some defects of the two.
  • Managing files with virtualenv in the past can be problematic, Pipenv uses Pipfile and, the latter stores the dependencies of packages, and viewing dependencies is very convenient.
  • Hash checks are used everywhere, both installing and uninstalling the package is very secure and automatically discloses security vulnerabilities.
  • Simplify development workflow by loading .env files.
  • Python2 and Python3 are supported, and the commands are the same on all platforms.

1. pip install pipenv

pip3 install pipenv  # Global installation, if you want to install only in current user mode, add the parameter: --user
 
# Updates
pip3 install --user --upgrade pipenv

Note: If pipenv is not available in the shell after installing in current user mode, you need to add the user library directory "C:\Users\XXX\AppData\Roaming\Python\Python37\Scripts" to the PATH [system environment path] of your computer and then restart your computer. PATH [system environment path], and then reboot your computer, you can use it after the environment variable takes effect.

2. pipenv virtual environment creation instructions

pipenv --two  				# Create an environment using Python2 on the current system
pipenv --three			    # Create an environment using Python3 on the current system
 
pipenv --python 3  			# Specify that the environment is created using Python3
pipenv --python 3.6  		# Specify to create an environment using Python 3.6
pipenv --python 2.7.14  	# Specify to create an environment using Python 2.7.14

1) When creating an environment, you should use a version of Python that is already installed on your system and can be searched for in the environment variables, otherwise you will get an error.

2) Each time the environment is created, a file named Pipfile will be generated in the current directory to record the information of the environment just created, and the file will be overwritten if it previously existed in the current directory.

3) When creating an environment with the specified version, there is a space between the version number and the argument --python.

3. Creating a Python 3.8 environment

Activate the virtual environment

pipenv shell 

Once the environment is activated, you will notice that the command prompt has changed (the prefix "pyTest" in the above image indicates that a virtual environment named "pyTest" has been generated).

4. Other common commands

pipenv --where                  # Display directory information
pipenv --venv  					# Display virtual environment information
pipenv --py  					# demonstratePythonInterpreter Information

pipenv install XXX  	  		# Install XXX module and add to Pipfile
pipenv install XXX==1.11  		# Install the fixed version ofXXXmodule and added to thePipfile

pipenv graph 					# View currently installed libraries and their dependencies
pipenv check  					# Check for security breaches
 
pipenv update --outdated  		# View all dependencies that need to be updated
pipenv update 					# Update all package dependencies
pipenv update package_name		# Update dependencies for the specified package
 
pipenv uninstall XXX 			# Uninstall the XXX module and remove it from the Pipfile
pipenv uninstall --all  		# Uninstall all packages and remove from Pipfile
pipenv uninstall --all-dev  	# Uninstall all development packages and remove from Pipfile

Exit the current virtual environment

exit 

Deleting a virtual environment

pipenv --rm

5. Running python files

# Run with pipenv full command
pipenv run python 
 
# Run in the activation environment
pipenv shell
python 

To this article on a simple introduction to the Python virtual environment and the use of this article is introduced to this, more related to the Python virtual environment content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!