What is a Virtual Environment (Virtualenv)?
When working with Python, you may encounter installation issues and permission errors. To address this, you can utilize virtual environments (virtualenvs), which serve as isolated Python installations.
Virtualenvs offer several benefits:
How to Create and Activate a Virtualenv
Python 3.3 :
python3 -m venv ENV_DIR
Windows:
C:\Python34\python.exe -m venv ENV_DIR
Older Python Versions:
virtualenv ENV_DIR venv ENV_DIR pyvenv ENV_DIR pyvenv3 ENV_DIR
To activate the virtualenv:
Unix:
. ./venv/bin/activate
Windows:
venv\Scripts\activate
The shell prompt will now display the virtualenv name to indicate which environment is active.
Using Virtualenv
Once the virtualenv is activated, you can install packages locally using pip:
(venv)$ pip install requests numpy
You can run Python commands within the virtualenv:
(venv)$ python [...] >>> import requests >>> import numpy as np
Deactivating Virtualenv
To exit the virtualenv:
(venv)$ deactivate
Managing Virtualenvs
You can create and remove virtualenvs as needed. To remove a virtualenv, simply delete the directory where it is located.
The above is the detailed content of What is a Virtual Environment and How Does it Help Python Developers?. For more information, please follow other related articles on the PHP Chinese website!