Creating and managing virtual environments in Python
![]() |
| Creating and managing virtual environments in Python |
Creating and managing virtual environments in Python
What is a Virtual Environment?
- A virtual environment is a self-contained directory that contains its own Python interpreter and can have its own installed packages.
Why Use Virtual Environments?
- Isolation: Prevent conflicts between different projects with different dependency versions.
- Cleaner Development: Avoid cluttering your global Python installation with project-specific packages.
- Dependency Management: Easily reproduce the environment on another machine.
2. Creating a Virtual Environment
- Using `venv` (built-in in Python 3.3 and newer)
# Create a virtual environment named 'myenv'
python3 -m venv myenv
Using `virtualenv` (install it first)
# Install virtualenv using pip
pip install virtualenv
# Create a virtual environment named 'myenv'
virtualenv myenv
3. Activating the Virtual Environment
- On Windows like using Command Prompt
myenv\Scripts\activate
On Windows (using PowerShell)
.\myenv\Scripts\Activate
On Unix or MacOS
source myenv/bin/activate
4. Deactivating the Virtual Environment
deactivate
5. Installing Packages
- With the virtual environment activated, you can use `pip` to install packages:
pip install package_name
6. Saving and Installing Dependencies
- Saving Dependencies to a File
- pip freeze > requirements.txt
Installing Dependencies from a File
pip install -r requirements.txt
7. Using Different Python Versions
- Specify the Python version when creating the virtual environment:
# Example for Python 3.8
python3.8 -m venv myenv
8. Removing a Virtual Environment
- Simply delete the virtual environment directory:
# On Unix or MacOS
rm -rf myenv
# On Windows
rmdir /s /q myenv
9. Checking the Python Version in a Virtual Environment
- Once your virtual environment is activated, you can check the Python version:
python --version
10. Virtual Environment Location
- By default, virtual environments are created in the same directory where you run the `python -m venv` or `virtualenv` command.
- You can specify a different location if needed.
# Example: Create a virtual environment in the parent directory
python -m venv ../myenv
11. Using `requirements.txt` for Dependency Management
- The `requirements.txt` file is a common way to specify project dependencies. Example `requirements.txt`:
requests==2.26.0
flask==2.1.0
Install dependencies from `requirements.txt`:
pip install -r requirements.txt
12. Using `Pipenv` for Dependency Management
- [Pipenv](https://pipenv.pypa.io/) is a tool that aims to bring the best of all packaging worlds to the Python world.
- It automatically creates and manages a virtual environment for your projects, as well as adds or removes dependencies from your `Pipfile` as you install/uninstall packages.
Install Pipenv:
- pip install pipenv
- Create a virtual environment and install dependencies:
- pipenv install
Activate the virtual environment:
- pipenv shell
13. Virtual Environment with Jupyter Notebooks
- You can use Jupyter notebooks within a virtual environment. First, install `ipykernel`:
- pip install ipykernel
- Then, add the virtual environment to Jupyter:
- python -m ipykernel install --user --name=myenv
14. Virtual Environment with Different Shells
- For `fish` shell:
- source myenv/bin/activate.fish
- For `csh` shell:
- source myenv/bin/activate.csh
15. Persistent Virtual Environments
- [Pipx](https://pipxproject.github.io/pipx/) allows you to install and run Python applications in isolated environments. It's especially useful for command-line tools.
- Install Pipx:
- pip install pipx
- Install a package using Pipx:
pipx install PACKAGE_NAME

.png)