Installing "virtual" typically refers to setting up a Python virtual environment, a self-contained directory that holds a specific Python interpreter and its own set of installed packages. This lets you work on multiple projects with different dependencies without conflicts. The process is straightforward and requires only a working Python installation and a command-line interface. Below is a step-by-step guide to creating, activating, and using a virtual environment, along with common pitfalls and troubleshooting advice.
What You'll Need
- A computer running Windows, macOS, or Linux.
- Python 3.3 or later installed. You can check by running
python --versionorpython3 --versionin your terminal or command prompt. - Access to a command-line interface (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows).
- Basic familiarity with navigating directories using
cd.
Step-by-Step
- Open your terminal or command prompt. On Windows, you can use Command Prompt or PowerShell; on macOS and Linux, open Terminal.
- Navigate to your project folder. Use
cd path/to/your/projectto move into the directory where you want the virtual environment. If the folder doesn't exist, create it first withmkdir myprojectand thencd myproject. - Create the virtual environment. Run
python -m venv venv(orpython3 -m venv venvon systems wherepythonpoints to Python 2). This creates a folder namedvenvcontaining a copy of the Python interpreter and thepippackage manager. You can name the environment anything you like, butvenvis conventional. - Activate the virtual environment. The activation command depends on your operating system and shell:
- Windows (Command Prompt):
venv\Scripts\activate.bat - Windows (PowerShell):
.\venv\Scripts\Activate.ps1 - macOS/Linux (bash/zsh):
source venv/bin/activate
(venv). - Windows (Command Prompt):
- Verify activation. Run
which python(macOS/Linux) orwhere python(Windows). The output should point to the Python executable inside yourvenvfolder, confirming the environment is active. - Install packages as needed. With the environment active, use
pip install package_nameto install any libraries. These will be placed in thevenvfolder and won't affect your global Python installation. - Work on your project. Run your Python scripts or start an interactive session with
python. All packages you install will be isolated to this environment. - Deactivate when done. Simply run
deactivateto return to your system's default Python. The environment remains on disk and can be reactivated later. - Recreate or remove the environment if needed. To start fresh, delete the
venvfolder and repeat from step 3. To remove it permanently, just delete the folder.
Common Mistakes
- Forgetting to activate the environment. If you install packages without activating, they go to your global Python and may cause conflicts. Always check for the
(venv)prefix in your prompt. - Using the wrong Python version. Creating a virtual environment with
pythonwhen you intended to use Python 3 can lead to unexpected behavior. Explicitly usepython3if necessary. - Not ignoring the environment folder in version control. The
venvdirectory should be added to.gitignoreto avoid committing thousands of files. - Assuming the environment is portable. Virtual environments are not meant to be moved or shared across machines; recreate them on each system.
- Running scripts with the system Python. If you double-click a script or use an IDE that isn't configured to use the virtual environment, it may run with the global interpreter. Configure your IDE to point to the
venvPython executable.
Tips
- Name your environment something descriptive, like
env_projectname, if you juggle multiple projects. - Use a
requirements.txtfile to record dependencies:pip freeze > requirements.txtand laterpip install -r requirements.txt. - On Windows, if you encounter execution policy errors in PowerShell, you may need to run
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned(after understanding the security implications). - Consider using
virtualenv(a third-party tool) if you need features not available in the standardvenvmodule, but for most usersvenvis sufficient. - Keep your virtual environment folder out of cloud sync services (like Dropbox) to avoid file locking issues.
Troubleshooting
- "python: command not found" or "python is not recognized". Python is not installed or not on your PATH. Install Python from python.org and ensure the "Add Python to PATH" option is checked during installation (Windows).
- Activation script cannot be loaded (PowerShell). You may need to adjust the execution policy as mentioned in Tips, or use Command Prompt instead.
- Permission denied when activating on macOS/Linux. Ensure the activation script is executable:
chmod +x venv/bin/activate. - pip install fails with permission errors. This usually means you're not in an active virtual environment and pip is trying to write to system directories. Activate the environment first.
- Environment seems broken after moving the folder. Virtual environments contain absolute paths; moving them breaks them. Delete and recreate.
- Packages not found after activation. Check that you installed them while the environment was active. Run
pip listto see what's installed.
Frequently Asked Questions
What is a virtual environment?
A virtual environment is a directory that contains a Python installation and a set of packages isolated from the global Python environment. It allows different projects to use different package versions without interference.
Do I need to install virtualenv separately?
No. Python 3.3 and later include the venv module in the standard library, so you can create virtual environments without installing anything extra.
Can I use multiple virtual environments at once?
You can have many virtual environments on your system, but only one can be active in a given terminal session at a time. Activating a new one replaces the previous activation.
How do I know if a virtual environment is active?
Your command prompt typically shows the environment name in parentheses, such as (venv). You can also run which python (macOS/Linux) or where python (Windows) to see which Python executable is in use.
What happens if I delete the virtual environment folder?
Deleting the folder removes the environment and all packages installed within it. Your project files remain, but you'll need to recreate the environment and reinstall dependencies if you want to run the project again.
Is it safe to commit the virtual environment to version control?
No. Virtual environments contain many files and absolute paths that are specific to your machine. It's standard practice to exclude the environment folder from version control and instead share a requirements.txt file.