Files
notes/Python-virtual-environment.md.md

30 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
To create a virtual environment, go to your projects directory and run the following command. This will create a new virtual environment in a local folder named .venv:
```bash
python3 -m venv .venv
```
The second argument is the location to create the virtual environment. Generally, you can just create this in your project and call it .venv.
venv will create a virtual Python installation in the .venv folder.
Note: You should exclude your virtual environment directory from your version control system using .gitignore or similar.
Activate a virtual environment
Before you can start installing or using packages in your virtual environment youll need to activate it. Activating a virtual environment will put the virtual environment-specific python and pip executables into your shells PATH.
```bash
source .venv/bin/activate
```
To confirm the virtual environment is activated, check the location of your Python interpreter:
```bash
which python
```
While the virtual environment is active, the above command will output a filepath that includes the .venv directory, by ending with the following:
'.venv/bin/python'
While a virtual environment is activated, pip will install packages into that specific environment. This enables you to import and use packages in your Python application.
Deactivate a virtual environment
If you want to switch projects or leave your virtual environment, deactivate the environment:
```bash
deactivate
```