• notice
  • Congratulations on the launch of the Sought Tech site

Python3 Creating a virtual environment

Recently, I am using python to develop some projects, and because there are many dependent packages, I use the dependent package index everywhere. If all the dependent packages are installed in the real environment, then all the dependent packages will be marked when packaging, which is troublesome. So the virtual environment is used, because the dependencies in the virtual environment are independent of each other.

1. Use venv

install module

python3 pip install virtualenv

Create a virtual environment

python3 - m venv venv_dir   # Create a virtual environment venv_dir, the venv_dir folder will be automatically generated

Activate the virtual environment

cd venv_dir/./ Scripts / activate pip install package

PS: After activating the environment, all operations are performed in the virtual environment, and will not go to the global python environment and other python virtual environments.

Exit the virtual environment

./Scripts/deactivate _ _ _

Delete the virtual environment

rm -rf venv_dir

PS: Delete the virtual environment directory to delete the virtual environment (installed python packages will be deleted)

2. Use anaconda's conda

Create a virtual environment

conda create - n env_name python = version package_names (the python package installed in this environment by default ) # Example conda create -- name django_venv python = 3.8 django

View virtual environment

conda env list # View all virtual environments created by conda

Activate the virtual environment

conda activate env_name # example conda activate django_venv

Exit the virtual environment

conda deactivate   # Execute conda deactivate directly to exit the current virtual environment

Delete the virtual environment

conda remove -n ven_name --all

3. Generate and install requirements.txt

Generate requirements.txt file

pip freeze > requirements.txt _

Install requirements.txt dependencies

pip install -r requirements.txt _


Technical otaku

Sought technology together

Related Topic


Leave a Reply