1

I am currently working on a python project using Visual Studio Code and Conda. When I try to install a package in the virtual environment it does not work (details and steps below). But if I use my computers command prompt and install that way, it does work in the virtual environment.

My steps are as follows:

I created a virtual environment as so:

conda create -n envname

Activated the environment:

conda activate envname

Tried to install a package (i tried to use conda install but package wasnt found):

pip install packagename 

Then write it into my code like so:

import packagename

Which throws me this error:

ModuleNotFoundError: No module named 'packagename'

I did try the solution from this page and tried installing the package this way:

C:\Users\myname\anaconda3\envs\envname\Scripts\pip install packagename

but it returns this:

Requirement already satisfied: packagename in c:\users\name\anaconda3\envs\envname\lib\site-packages

when i type:

conda list

The package I installed is included, as well as pip. However, the package I installed shows the version # in one column, then only "pypi_0 pypi" in the last column. not sure if that means something is wrong or not.

packagename                  3.41                 pypi_0    pypi

As mentioned, if I type this in my computers command prompt (separate from VSC and my virtual environment):

pip install packagename

My virtual environment will pick it up as being installed.

I feel all mixed up lol, like some path somewhere isnt right but I cant figure out what

Any ideas?

1 Answers1

0

No Need to explicitly activate the venv

Try using following commands.

Step 1: Create virtual env
python -m venv envname

Step 2: Switch to this newly created virtual env
Windows
.\envname\Scripts\activate
Unix
source envname/bin/activate

Step 3: install packages in the virtual env directory
pip install <packagename>

  • I tried the exact steps as above in a completely fresh folder, and i am getting the same error, maybe i am doing it wrong? i switched my interpreter from conda to straight python, then created a new folder for this test project, created a test .py file -> right click and open in integrated terminal -> the env is created in my proj folder -> followed the steps above -> same error. pip list in the new env shows packages were installed, running script produces the same error! – hbkpancakes Nov 26 '20 at 17:44
  • actually i think i got it --- after i created the environment, i did this is visual studio code: ctrl + shift + p -> select interpreter -> then i went to my newly created environment and set that python.exe as the interpreter and now its working. i never heard of having to do that before, does that sound right? – hbkpancakes Nov 26 '20 at 17:55