Reputation: 549
I have a written a makefile to use to run Python code. The idea is it will allow me to run my linter, pytest, and run the code all in one command. Importantly, it will also spin up a python environment to use.
An excerpt of it looks like:
VENV := venv
all: venv
$(VENV)/bin/activate: requirements.txt
python3 -m venv $(VENV)
./$(VENV)/bin/pip install -r requirements.txt
# venv is a shortcut target
venv: $(VENV)/bin/activate
run: venv
flake8 --exclude=venv
pytest --ignore=venv
mypy advent_of_code/dayone.py
python advent_of_code/dayone.py
The rest of the code is found here: https://github.com/andrewblance/advent_of_code_2020
However, when I run my code I do not think it is using this environment. I think it still uses my default python environment. When I run this code I can see the version of Pytest and mypy that is being used depends on what is installed on the default python environment.
Have I done something wrong that means when I run flake & etc that I don't use the new environment? How can I change it so it only uses the environment specified in the makefile, and how do I "turn off" the environment once I am done with it?
Upvotes: 0
Views: 520
Reputation: 100781
Short: you can't.
Longer: every process has its own environment. Its environment is inherited from the process that started it. But, it's impossible for a child process to modify or change the environment of its parent.
Every command is a process. So the make
program is a process, and each command line that make invokes is a process. So when you run the command python3 -m venv $(VENV)
that starts a shell process, which runs python3 -m venv ...
which is another process. Then whatever change to the environment python3
made is lost when python3
exits, and whatever change to the environment was made in the shell that started python3
is lost when the shell exits, and then other shells are started with other commands, and when all the commands are done make
will exit and any changes made to its environment are lost when you get back to your shell prompt.
Upvotes: 2