Python Virtual Environment


0:00
0:00

Python Environment


1. Python Virtual Environment (venv) - Built-in Solution

CommandDescriptionnpm/Other Comparison
python -m venv myenvCreates a new virtual environment using the Python version that executed the commandSimilar to npm init but only creates environment structure
python3.9 -m venv myenvCreates 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 environmentNo npm equivalent (npm uses local node_modules automatically)
myenv\Scripts\activate (Windows)Windows version of activate commandNo npm equivalent
pip install package_nameInstalls a package in the active environmentnpm install package_name
pip freeze > requirements.txtSaves list of installed packagesSimilar to npm updating package.json
pip install -r requirements.txtInstalls packages from requirements filenpm install
pip listShows installed packagesnpm list
python -m pip install --upgrade pipUpgrades pip within the active virtual environment, else globalnpm update npm (else npm update npm -g)
deactivateExits the virtual environmentReturns to system shell
rm -rf myenv/ (Linux/Mac)Deletes the environmentSimilar to deleting node_modules
rmdir /s myenv (Windows)Windows command to delete environmentSimilar to deleting node_modules

2. Conda Environment - Better for Version Management

CommandDescriptionAvailability
Download and run conda installerInstall conda first timeDownload & 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 condaUpdate conda
conda create -n myenv python=3.7Creates environment with specific Python versionMain advantage over venv - can specify any Python version
conda activate myenvActivates the conda environmentWorks on all platforms with same command
conda install package_nameInstalls a packageCan install both Python and non-Python packages
conda list --export > requirements.txtExports environment packagesSimilar to pip freeze
conda env create -f environment.ymlCreates environment from YAML fileMore powerful than requirements.txt
conda listLists installed packagesShows conda-specific and pip-installed packages
conda deactivateExits the conda environmentReturns to base conda or system
conda env remove -n myenvRemoves the environmentCleans up completely
conda update condaUpdates conda itselfSelf-maintenance

3. Docker for Python Development

CommandDescriptionBenefits
docker pull python:3.9Pulls Python image of specific versionComplete isolation with exact version control
docker build -t myproject .Builds container from DockerfileCreates reproducible environment
docker run -it myprojectRuns the container interactivelyConsistent environment across all machines
docker-compose upStarts services defined in docker-compose.ymlManages multiple containers/services
docker exec -it container_name pip install packageInstalls package in running containerModifies environment in real-time
docker cp requirements.txt container:/app/Copies files into containerTransfers dependency files
docker commit container_id new_imageSaves container state as new imageCaptures environment changes

Last updated on August 15, 2025

🔍 Explore More Topics

Discover related content that might interest you

TwoAnswers Logo

Providing innovative solutions and exceptional experiences. Building the future.

© 2025 TwoAnswers.com. All rights reserved.

Made with by the TwoAnswers.com team