Python Virtual Environment
0:000:00
Python Environment
1. Python Virtual Environment (venv) - Built-in Solution
Command | Description | npm/Other Comparison |
---|---|---|
python -m venv myenv | Creates a new virtual environment using the Python version that executed the command | Similar to npm init but only creates environment structure |
python3.9 -m venv myenv | Creates environment with specific Python version (if that version is installed) | Similar to using nvm to select Node version |
source myenv/bin/activate (Linux/Mac) | Activates the virtual environment | No npm equivalent (npm uses local node_modules automatically) |
myenv\Scripts\activate (Windows) | Windows version of activate command | No npm equivalent |
pip install package_name | Installs a package in the active environment | npm install package_name |
pip freeze > requirements.txt | Saves list of installed packages | Similar to npm updating package.json |
pip install -r requirements.txt | Installs packages from requirements file | npm install |
pip list | Shows installed packages | npm list |
python -m pip install --upgrade pip | Upgrades pip within the active virtual environment, else global | npm update npm (else npm update npm -g ) |
deactivate | Exits the virtual environment | Returns to system shell |
rm -rf myenv/ (Linux/Mac) | Deletes the environment | Similar to deleting node_modules |
rmdir /s myenv (Windows) | Windows command to delete environment | Similar to deleting node_modules |
2. Conda Environment - Better for Version Management
Command | Description | Availability |
---|---|---|
Download and run conda installer | Install conda first time | Download & install from: - Anaconda (full distribution with many packages) https://www.anaconda.com/download or, - Miniconda (minimal installation) https://docs.conda.io/en/latest/miniconda.html Add conda to PATH |
conda install conda | Update conda | |
conda create -n myenv python=3.7 | Creates environment with specific Python version | Main advantage over venv - can specify any Python version |
conda activate myenv | Activates the conda environment | Works on all platforms with same command |
conda install package_name | Installs a package | Can install both Python and non-Python packages |
conda list --export > requirements.txt | Exports environment packages | Similar to pip freeze |
conda env create -f environment.yml | Creates environment from YAML file | More powerful than requirements.txt |
conda list | Lists installed packages | Shows conda-specific and pip-installed packages |
conda deactivate | Exits the conda environment | Returns to base conda or system |
conda env remove -n myenv | Removes the environment | Cleans up completely |
conda update conda | Updates conda itself | Self-maintenance |
3. Docker for Python Development
Command | Description | Benefits |
---|---|---|
docker pull python:3.9 | Pulls Python image of specific version | Complete isolation with exact version control |
docker build -t myproject . | Builds container from Dockerfile | Creates reproducible environment |
docker run -it myproject | Runs the container interactively | Consistent environment across all machines |
docker-compose up | Starts services defined in docker-compose.yml | Manages multiple containers/services |
docker exec -it container_name pip install package | Installs package in running container | Modifies environment in real-time |
docker cp requirements.txt container:/app/ | Copies files into container | Transfers dependency files |
docker commit container_id new_image | Saves container state as new image | Captures environment changes |