Creating a virtual environment with Python 3 isolates project dependencies and keeps your global system clean. This approach ensures reproducible builds and simplifies collaboration across different machines.
Use Python 3 virtualenv to manage package versions per project, avoid conflicts, and mirror production setups during development. The following sections explain commands, best practices, and common scenarios.
| Action | Command (example) | Purpose | Notes |
|---|---|---|---|
| Install virtualenv | pip install virtualenv | Add the tool to your Python environment | Use user flag if avoiding system changes |
| Create environment | virtualenv -p python3 myenv | Set up isolated folder with Python 3 | Replace myenv with your chosen name |
| Activate on Linux/macOS | source myenv/bin/activate | Switch shell to use the virtual environment | Prompt changes to show (myenv) |
| Activate on Windows | myenv\Scripts\activate | Enable environment in PowerShell or CMD | Execution policy may require adjustment |
| Save dependencies | pip freeze > requirements.txt | Record exact package versions | Commit requirements.txt to version control |
Setting Up Python 3 Virtualenv
Start by installing virtualenv at the system level to keep the tool available across projects. You can then create a new environment that points specifically to your installed Python 3 interpreter.
Install the package
Run pip install virtualenv with appropriate permissions. On most systems, this adds the virtualenv binary to your PATH.
Create an isolated folder
Use virtualenv -p python3 myproject_env to generate a clean directory containing its own Python binary and pip. This keeps site-packages separate from any system-wide libraries.
Activating and Using the Environment
Activation adjusts your shell PATH so that python and pip point inside the virtual environment instead of the global installation. Deactivation restores the original PATH when you finish working.
Linux and macOS activation
Execute source myproject_env/bin/activate and confirm the prompt updates to show the environment name.
Windows activation
Run myproject_env\Scripts\activate in Command Prompt or PowerShell, then verify that which python resolves to the environment folder.
Managing Dependencies
Within an active virtual environment, pip install adds packages only to that environment, avoiding interference with system tools. You can export and later restore exact versions to maintain consistent behavior across setups.
Installing packages
Use pip install requests flask or any package name inside the activated environment to keep dependencies scoped.
Freezing the state
Run pip freeze > requirements.txt to capture current versions, then install with pip install -r requirements.txt on fresh clones or servers.
Best Practices and Organization
Following consistent patterns reduces mistakes when switching between projects and simplifies onboarding for new contributors.
- Place requirements.txt in version control to lock dependency versions.
- Name environments with clear identifiers like projectname_env.
- Exclude the environment folder from git by adding it to .gitignore.
- Recreate environments from requirements.txt on new machines.
- Update packages deliberately and test before upgrading in production.
Maintaining Reproducible Workflows
Treating your virtual environment and requirements.txt as part of the codebase ensures that development, testing, and deployment stay aligned across teams and machines.
FAQ
Reader questions
Should I use venv or virtualenv for Python 3 projects?
Use virtualenv if you need additional features or broader Python version support; otherwise, the built-in venv module is sufficient for most Python 3 cases.
What happens if I forget to activate the environment before running pip?
Packages install globally or into the wrong location, potentially breaking system tools and making dependency tracking harder.
Can multiple projects share the same virtual environment safely?
Sharing one environment across projects increases conflict risk; prefer one environment per project to keep requirements isolated.
How do I remove a virtual environment cleanly?
Deactivate first, then delete the environment folder and remove any references from requirements.txt or documentation.