Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 937
Andy Brown Avatar asked Oct 18 '25 07:10

Andy Brown


1 Answers

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

like image 150
Andy Brown Avatar answered Oct 20 '25 21:10

Andy Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!