Reputation: 501
When we want to do anything with our python virtual environment in terminal/command prompt/shell, we have to activate it by navigating to the scripts folder. But, what does this activation do?
I can access the contents of the virtualenv folder in file explorer without activating it.
Upvotes: 6
Views: 5013
Reputation: 2347
When you activate your virtual environment (venv), at least in 2024, using source bin/activate
, this simply switches your command line to the command line of the venv.
For example, if I have an exampleApp
project and I open the terminal (bash) to the project, the command line might look like the following to let me know that I am at the root of my project directory using the system tools of my local default environment.
me@MYCOMPUTER:/mnt/c/Users/me/LocalDev/exampleApp$
Assuming I have a .venv
directory within that project I can run the command source .venv/bin/activate
which changes my command line to:
(.venv) me@MYCOMPUTER:/mnt/c/Users/me/LocalDev/exampleApp$
You can think of your venv as a new container to run your project (similar to running a project with Docker where you can run special tools that not installed in your default environment). If I run the ls
command (assuming a unix shell prompt) I still see the list of files/directories at the root of my project. The only difference now is that if I run any Python scripts, those scripts will use the version of Python and the Python packages installed in the venv. If I run pip install markdown2
while the venv is activated, this will install the markdown2 plugin within my venv and not outside of it.
However you only may only need to activate a venv if you want to manually run commands via the shell prompt, such as installing additional plugins, and utilize the tools available in the venv. If you do not need to do any of these things, another approach is to use a Makefile to process Python scripts using the venv. An example Makefile might look like:
Makefile
# Variables
PYTHON = python3
VENV_DIR = .venv
# Commands
venvSetup:
$(PYTHON) -m venv $(VENV_DIR)
pip install --upgrade pip
$(VENV_DIR)/bin/pip install -r requirements.txt
run:
$(VENV_DIR)/bin/python runserver.py
Then you can simply run make venvSetup
to perform the commands listed under the venvSetup
section of the Makefile, and make run
to perform the command listed under the run
section of the Makefile. This allows you to use the venv but does not require you to manually run source bin/activate
and then deactivate
to stop using the venv.
Upvotes: 0
Reputation: 148
At its core, the main purpose of Python virtual environments is to create an isolated environment for Python projects. This means that each project can have its own dependencies, regardless of what dependencies every other project has.
More interesting are the activate scripts in the bin directory. These scripts are used to set up your shell to use the environment’s Python executable and its site-packages by default.
After activating the environment, we’re now getting a different path for the python executable because, in an active environment, the $PATH environment variable is slightly modified.
Upvotes: 9