3

Although I'm enjoying developing in Python in Visual Code, I'm finding managing virtual environments and packages frustrating, and particularly am struggling with installing packages in the right place. Here's my sequence of steps, and the problem I then have - I wonder if anyone could kindly tell me where I've gone wrong? Or do I really need to include the full Python path?

So first I create a new virtual environment:

enter image description here

I can see that this works:

enter image description here

I then choose to use the Python interpreter in this new virtual environment (I can't quite see why I have to do this - surely this should happen as part of the activation process - but I can live with it):

enter image description here

At the bottom left corner of my screen, I get the reassuring fact that I'm using the right Python interpreter:

enter image description here

I then install a package (I've chosen requests more or less at random):

enter image description here

However, this is going in my default Python location. To get it in my new virtual environment, I seem to have to include the full path to the Python interpreter:

enter image description here

This can't be right, although it does work - I can now see the installed package:

enter image description here

Can anyone help please?

Andy Brown
  • 5,309
  • 4
  • 34
  • 39

3 Answers3

2

OK, thanks to Jason/Steven I have finally got the hang of what you should be simple, but isn't. Here's what I reckon is the easiest way to create and activate a Python virtual environment in Visual Studio code. Let's say I start with the Tutorial environment active, and want to create one called ForeignHoliday (we can but dream). Start by creating the new environment in the VS Code terminal:

enter image description here

This creates the environment:

enter image description here

However, it doesn't activate it, nor does it change the default Python interpreter to use the one for the new virtual environment. You can do both of these things in one go by choosing an interpreter - click here at the bottom left of the VS Code screen:

enter image description here

You can now select an interpreter - your new virtual environment (irritatingly) won't be listed yet, so you'll have to find it:

enter image description here

Choose to find your interpreter:

enter image description here

Double-click on the Python interpreter in the Scripts folder in your new virtual environment (the pythonw alternative doesn't invoke a terminal window, so most people should avoid this - see this SO article):

enter image description here

Now press SHIFT + CTRL + ' to start a new terminal window (NOT just CTRL + ', as this switches you to an existing terminal window). You should see this:

enter image description here

You can now install and import packages and they will all be in the right place! I wish I'd read this answer a few days ago ...

Andy Brown
  • 5,309
  • 4
  • 34
  • 39
1

Personally I haven't had luck using PowerShell (due to permissions to run PowerShell scripts) so I use Command Prompt in VS Code instead.

For PowerShell, perhaps activating your environment using Scripts\activate.ps1 will work instead. From the docs at: https://docs.python.org/3/library/venv.html

# PS C:\> <venv>\Scripts\Activate.ps1
StackOverflowExample\Scripts\Activate.ps1

It is a bit confusing in VSCode having an interpreter selected and a different Command Prompt/Power Shell terminal used to install packages into a virtual environment.

Another confusing point is running StackOverflowExample\Scripts\activate doesn't suggest you are doing anything is wrong.

Jason Cook
  • 1,236
  • 9
  • 12
  • I can't believe that a) VS Code makes it so hard to do this and that b) everybody else doesn't have problems with this! Activate.ps1 is great - it actually switches to the environment. I can then install packages, and they show up in the folder lib\site_packages. But ... when I try to import them, they're not recognised by Intelllisense or the Python compiler (it's as if they're not there). I'm definitely creating and running .py files in the right environment - what other tiny thing might I be doing wrong please? – Andy Brown Jul 15 '21 at 09:32
1

I agree with Jason Cook. activate.bat used to activate the environment in Cmd, you should take Activate.ps1 instead of activate.bat.

But you need not activate the environment by yourself. The Python extension can choose the right one to activate the environment for you when you create a new terminal.

After you select the interpreter, you need to create a new terminal. In general, we take a shortcut of Ctrl+Shift+`.

And if you want to turn off this function, you can set this in the settings.json:

"python.terminal.activateEnvironment": false,
Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13
  • I don't quite understand this. When I create a new terminal, nothing asks me which environment I want to use (it just uses the active one). I've checked my settings, and I haven't turned this setting off as you suggest is possible. – Andy Brown Jul 15 '21 at 09:34
  • @Andy Brown Yes, when you create a new terminal, the python extension will activate the environment in it automatically. Then when you run the commands in the terminal, it will select the right python interpreter, instead of you appoint it like 'c:\ajb\Stack...'. – Steven-MSFT Jul 15 '21 at 10:01
  • @Andy Brown Have you tried the shortcut? And does it work? – Steven-MSFT Jul 15 '21 at 10:03
  • I've been using the short-cut for a while - and yes, it works. It appears that if I select a Python interpreter tied to virtual environment V1 then activate virtual environment V2, when I create a new terminal Visual Studio code automatically assumes I'm using the original selected Python interpreter and switches me back to V1. This means that to activate a virtual environment you need to activate it using activate.ps1 and then choose the default Python interpreter. Surely it can't be this complicated? – Andy Brown Jul 15 '21 at 10:06
  • Actually, I now get what you mean - choosing an interpreter automatically activates the environment! – Andy Brown Jul 15 '21 at 10:08