📅  最后修改于: 2023-12-03 15:34:05.685000             🧑  作者: Mango
Virtual environments are a way to create isolated Python environments for different projects. This allows you to have different versions of Python and/or different dependencies for each project without them interfering with each other. In this article, we will cover the basics of creating and using virtual environments in Python.
Virtual environments come built-in with Python 3.3 and greater. If you are using an older version of Python, you can install virtual environments using pip:
pip install virtualenv
To create a new virtual environment, run the following command:
python -m venv /path/to/new/virtual/environment
This will create a new folder at /path/to/new/virtual/environment with a fresh copy of Python and pip installed.
To activate a virtual environment, navigate to its directory and run the activate script:
source /path/to/virtual/environment/bin/activate
You will now be in the virtual environment and any packages installed using pip will only be available within this environment.
To deactivate the virtual environment, simply run:
deactivate
To install a package in a virtual environment, simply activate the environment and use pip:
pip install package_name
Virtual environments are a powerful tool for managing Python projects, allowing you to keep dependencies and versions isolated. By following the steps outlined in this article, you should now be able to create and use virtual environments in your Python projects.