Python Forum
Virtual Environment (using VS Code) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Virtual Environment (using VS Code) (/thread-39414.html)



Virtual Environment (using VS Code) - Ashcora - Feb-14-2023

Hello community,

I am trying to figure out which is the best way to use virtual environments…as there is mamba, conda, miniconda, anaconda and venv (…) I started to struggle a little…for writing code I am using Visual Studio Code. The code is more likely little scripts to make life easier (like small automation) but with different context Smile my „dream goal“ would be to have the following solution: for each script (or its project folder?), I want VS Code to recognize the script (or the folder of the script) as virtual environment and to switch it (automatically?) when opening the script. Which is the best way to go here? Can someone guide me? Your support would be really appreciated.


RE: Virtual Environment (using VS Code) - Larz60+ - Feb-14-2023

I use the following method for a new project:

  1. Create a new directory and make cwd
  2. create the virtual directory I use: python -m venv 'Virtual project name'
  3. You can activate virtual environment inside of VSCode:
    • Ctrl/Shift/P
    • click on select interpreter
    • choose your newly created virtual environment (will be remembered, for all code in project)
  4. when using python from command line, start virtual environment as follows:
    1. Set cwd to top directory of project
    2. Issue command (Linux) . ./venv/bin/activate



RE: Virtual Environment (using VS Code) - Ashcora - Feb-14-2023

(Feb-14-2023, 11:56 AM)Larz60+ Wrote: I use the following method for a new project:

  1. Create a new directory and make cwd
  2. create the virtual directory I use: python -m venv 'Virtual project name'
  3. You can activate virtual environment inside of VSCode:
    • Ctrl/Shift/P
    • click on select interpreter
    • choose your newly created virtual environment (will be remembered, for all code in project)
  4. when using python from command line, start virtual environment as follows:
    1. Set cwd to top directory of project
    2. Issue command (Linux) . ./venv/bin/activate

Thanks for your quick response. I tried with venv (as you described) and also miniconda (with having the separate "Python selection" Addin in VS Code) using "open in terminal" resp. "set as active workspace interpreter".

Is there any possibility to enable VS Code to recognize which interpreter/virtual environment has to be used? (e.g. when selecting a *.py in a respective folder)


RE: Virtual Environment (using VS Code) - deanhystad - Feb-14-2023

Quote:Is there any possibility to enable VS Code to recognize which interpreter/virtual environment has to be used? (e.g. when selecting a *.py in a respective folder)
Kind of. When you open a folder in VSCode, that folder is treated as the top of the project. You can then specify the Python interpreter to use with that project.

Inside the same source tree you can open a different folder and set a different Python interpreter. Depending on what folder you "open", in VSCode, you get a different interpreter.

For example, I have a mucking about folder I call "musings". Under musing I have a "pygames" folder and a "qt" folder. I have a virtual environment that I use for the musings folder that ends up being a catch-all that I blast from time to time and restart. The pygames folder has it's own virtual environment that has a bunch of pygame packages installed. qt has a different virtual environment that has qt related packages installed.

So you cannot bind an interpreter to a particular python file, but you can bind an interpreter to a particular folder.


RE: Virtual Environment (using VS Code) - snippsat - Feb-15-2023

(Feb-14-2023, 12:35 PM)Ashcora Wrote: Is there any possibility to enable VS Code to recognize which interpreter/virtual environment has to be used? (e.g. when selecting a *.py in a respective folder)
I usally start VS Code from folder i want to work with,from command line with code .
This also apply to virtual environment folder with venv or Anaconda/Miniconda.
If you look at this post,ther i use a conda enviroment
A example from start with venv.
[Image: Ql3q7d.png]
This start VS Code where it find the enviroment.
[Image: AOSeJv.png]