9

I am running into trouble running a python program inside visual studio code on my Windows 10 PC.

When I start a python session from the terminal and say import cv2 I get no errors:

enter image description here

When I go through the command prompt and start VSCode from within the env, I get the ModuleNotFoundError

enter image description here

Traceback (most recent call last): File "gui.py", line 3, in <module> import cv2 ModuleNotFoundError: No module named 'cv2'

When I try to run the program as python gui.py from within the environment my command prompt crashes with the error message:

enter image description here

I'm not sure how to ask this question other than wondering is there a way to start my IDE from within the python env so that it can locate all the local packages.

Q.H.
  • 1,406
  • 2
  • 17
  • 33
  • Did you check to make sure that cv2 is installed in that environment? Is it in `pip freeze`? – addohm May 16 '18 at 00:32
  • 1
    I can't really tell what command prompt you're executing from. Either way, my process is typically 1) `./project/scripts/activate.ps1` (or `activate.bat` depending on your flavor) then 2) `code .` Code is typically added to your $PATH$ environment variable, so you do not need to cd all the way into that directory. – addohm May 16 '18 at 00:36
  • @Jaberwocky "code" as in VS Code correct? – Q.H. May 16 '18 at 14:37
  • 1
    Yes. If you type code in at command prompt. VS Code will start, assuming you chose the default installation selections. – addohm May 16 '18 at 15:12
  • Also keep in mind, I said `code .` which would be executed from within the project folder. You can type `code` without the `.` from anywhere. The `.` just tells VS Code to open that directory. As an example. you can type `code /path/to/project` or `code ~/foo/bar.txt` – addohm May 16 '18 at 15:17
  • Also, if you're starting your virtual environment from within Anaconda (command prompt terminal), you do not need to run `activate.ps1` or `activate.bat`. Just `cd` into the project directory, and type `code .`. – addohm May 16 '18 at 15:19

1 Answers1

21
  1. Make sure you have visual studio code installed in your system.
  2. Make sure your environment variables are configured. (see below image for reference) enter image description here

  3. Go to your Anaconda command prompt, and type code --version (see below image for reference) enter image description here

  4. To Launch VS code from Anaconda/Window command prompt type code. (see below image)

enter image description here

Manjeet
  • 949
  • 14
  • 23