30 lines
1.4 KiB
Markdown
Executable File
30 lines
1.4 KiB
Markdown
Executable File
To create a virtual environment, go to your project’s 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 you’ll need to activate it. Activating a virtual environment will put the virtual environment-specific python and pip executables into your shell’s 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
|
||
``` |