Problems installing Python packages into a virtual environment in Visual Studio Code

Viewed 1760

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?

3 Answers

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.

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,

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 ...

Related