Have you ever encountered situations like this: one project needs Django 3.2, but another requires Django 4.0, resulting in version conflicts and program crashes? Or perhaps you have an old project using Python 3.7 while a new project needs Python 3.11, but you can only install one Python version on your system? All these issues can be solved with virtual environments. Today, I'll help you thoroughly understand all aspects of Python virtual environments.
When it comes to virtual environments, many people's first reaction might be "it's too complicated." However, once you understand how they work, you'll find this tool is actually quite simple and extremely practical.
A virtual environment is essentially creating an independent "room" on your computer. This room has its own Python interpreter and dependencies, completely isolated from other parts of the system. You can think of it as a "miniature" Python installation environment.
I recall a typical case last year when working with students on a project. One student had installed TensorFlow 2.0 on their computer but discovered the course required the 1.x version API. Downgrading TensorFlow in the global environment would inevitably affect other ongoing projects. We ultimately solved this problem perfectly using a virtual environment.
Let's create a virtual environment. First, we need to confirm if Python is installed. Open the terminal (Command Prompt for Windows users) and enter:
python --version
Seeing a version number indicates correct installation. Next, let's create a virtual environment. I suggest giving virtual environments meaningful names, like project names, for easier management. For example, let's create a virtual environment named "data_analysis":
python -m venv data_analysis
What does this command do? It creates a folder named "data_analysis" in the current directory, containing a complete Python environment. You can check this folder and find directories like bin (Scripts on Windows), include, lib, etc.
Now we need to activate this virtual environment. The activation command differs slightly across operating systems:
Windows:
data_analysis\Scripts\activate
macOS/Linux:
source data_analysis/bin/activate
After activation, you'll notice "(data_analysis)" appears before the command line, indicating you've entered the virtual environment. All packages installed now will be confined to this environment.
Speaking of practical applications, let me share a real case from my work. Last year, I took over an old project that used Python 3.7 and Django 2.2. However, I had Python 3.11 installed on my computer, and other ongoing projects depended on this version.
The solution was as follows: First, create a dedicated virtual environment:
python3.7 -m venv legacy_project
After activating the environment, install project dependencies:
pip install -r requirements.txt
This perfectly resolved the version conflict issue. Old and new projects could coexist peacefully on the same computer without interference.
Through my experience with virtual environments, I've compiled some useful tips to share:
django_test (Django testing environment)
Dependency Management When creating a new project, the first task is to record dependencies:
pip freeze > requirements.txt
This command generates a file containing all dependencies. When you need to recreate the environment on another computer, simply use:
pip install -r requirements.txt
Let's discuss some more professional topics regarding advanced usage.
pyenv install 3.7.9
pyenv install 3.8.10
pyenv install 3.9.5
Then create separate virtual environments for each version:
pyenv local 3.7.9
python -m venv env37
pyenv local 3.8.10
python -m venv env38
pyenv local 3.9.5
python -m venv env39
import os
import sys
import subprocess
def create_venv(name):
subprocess.run([sys.executable, '-m', 'venv', name])
requirements = [
'pip-tools',
'black',
'flake8',
'pytest'
]
activate_script = os.path.join(name, 'Scripts' if os.name == 'nt' else 'bin', 'activate')
pip = os.path.join(name, 'Scripts' if os.name == 'nt' else 'bin', 'pip')
subprocess.run(f'"{pip}" install --upgrade pip', shell=True)
for req in requirements:
subprocess.run(f'"{pip}" install {req}', shell=True)
print(f'Virtual environment "{name}" created successfully')
print(f'To activate, run: source {activate_script}')
if __name__ == '__main__':
if len(sys.argv) != 2:
print('Usage: python create_venv.py <venv_name>')
sys.exit(1)
create_venv(sys.argv[1])
During teaching, I've noticed students often encounter certain issues. Here are some solutions I've compiled:
Set-ExecutionPolicy RemoteSigned
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
import sys
print(sys.prefix)
If the output path is the virtual environment's path, you're currently in the virtual environment.
Virtual environment technology continues to evolve. For instance, more people are starting to use Poetry for Python project management, which offers a more modern approach to dependency management. Docker container technology also provides another option for environment isolation.
What improvements do you think virtual environment technology still needs? Feel free to share your thoughts and experiences in the comments.
Through this article, we've deeply explored various aspects of Python virtual environments. From basic concepts to practical applications, from common issues to advanced techniques, I hope this helps you better understand and use this powerful tool.
Remember, using virtual environments isn't about showing off technical skills; it's about making our development work more standardized and efficient. As I often tell my students: developing the habit of using virtual environments is just as important as writing unit tests.
Do you have any insights or confusion about using virtual environments? Feel free to share in the comments.