Files
notes/documents/python/Python-virtual-environment.md
2024-08-10 00:08:43 -05:00

1.4 KiB
Executable File
Raw Blame History

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:

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.

source .venv/bin/activate

To confirm the virtual environment is activated, check the location of your Python interpreter:

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:

deactivate