How to configure a .env file properly to locate and use Python Interpreter in VSCode

Viewed 584

I am currently working on multiple python projects for multiple clients. So for a Client, my folder structure looks like the following:

Client 1
  -+ .venv
  -+ Project 1
       -- .env
          ...
  -+ Project 2
       -- .env
          ...
  -+ Project 3
       -- .env
          ...

Same goes for other clients. I don't (need to) create separate virtual environments (.venv) for separate projects, rather only for separate clients. Also, whenever I work on a project, I open the project folder using VSCode, not the client folder, making the project folder my workspace.

My objective here is that, every time I switch / open a project in new window in VSCode, and run a python script / jupyter notebook, it should select the python interpreter from the virtual environment in it's parent directory.

My current setup to solve it is to put a .env file in the project folder. The content of which is

PYTHON_INSTALL_LOC=Absolute path to/Client 1/.venv/Scripts/python.exe

and then, specifying

"python.envFile": "${workspaceFolder}/.env",
"python.defaultInterpreterPath": "${env:PYTHON_INSTALL_LOC}",

in the VSCode settings.json file.

But still whenever I change to different projects, and run a Jupyter Notebook, VSCode asks me to select interpreter.

So, is this possible to do with the current approach? Any other solution is also appreciated. Thanks.

0 Answers
Related