3

I have been using vsCode for my HTML edits and I have just started using python 3.8 and anaconda to make some graphs for work. I have been succesfull in using anaconda with the IDE that came with it during download, Spyder. But not a single one of the import packages work when i try the same code in vsCode.

this is the simple code ive been trying while practicing in spyder and vsCode:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 20, 100)  # Create a list of evenly-spaced numbers over the range
plt.plot(x, np.sin(x))       # Plot the sine of each x point
plt.show()                   # Display the plot

The error I keep getting is:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 24, in <module>
    from . import multiarray
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\multiarray.py", line 14, in <module>
    from . import overrides
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\overrides.py", line 7, in <module>
    from numpy.core._multiarray_umath import (
ImportError: DLL load failed: The specified module could not be found.

There is more telling me to re install numpy, which I did and it still didn't work. I have been reading loads and watching videos and tutorials to try and make this work. I do everything the same, like the installation process, but on my vsCode the python interpreter is different by showing "python3.7.4 base:conda" which is different from everything else.

anything will help, thanks for taking the time to look at this issue. sorry if its mundane.

here is a picture, may explain my problem better then me:

terminal in vscode

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Nestly
  • 41
  • 2
  • 6

4 Answers4

0

Reinstall those packages with conda install option. For numpy in this case:

https://anaconda.org/anaconda/numpy

Gert
  • 3
  • 1
  • I did that and still same issue. could this be a power shell issue, the first line comment "conda" is not recognized... – Nestly May 09 '20 at 21:27
  • You need to add conda to your paths. To do that: Type "Edit the system environment variables" to your search bar and open that. Find the option called "Environment Variables" and click on that. Choose the PATH variable and add "your" anaconda location to that variable. For example:(change the path below to your actual path) In this case you need to add these 2 paths. C:\Anaconda3 and C:\Anaconda3\Scripts – Gert May 09 '20 at 21:40
  • Have you set up a virtual environment to run from? – Natsfan May 09 '20 at 22:28
  • @jmh Ive been working on this all day. so I have just recently learnt about setting up a virtual environment. i am playing around with that on Anaconda prompt. and when i install all the packages i need it works great. next step, try and do this in vscode – Nestly May 09 '20 at 22:43
  • @gert okay i would just be adding new paths. the current one runs to python38, which i think is still important for running another program in python if i decide to start expanding from just scientific scrips. correct me if i'm wrong on any of this. sorry farily new to lots of this. big learning curve – Nestly May 09 '20 at 22:45
  • You don't need to do this in vc. Activate your v. envmnt. and then run vc. that should do it! – Natsfan May 09 '20 at 23:47
0

The new version of Anaconds no longer installs numpy. You need to run the following when your virtual environment has been activated. Run this from command line.

conda install -n yourenvname numpy  

where yourenvname is the name of you virtual environment. I hope this helps. It should. If it doesn't leave a comment and I'll give you something else that may help.

Natsfan
  • 4,093
  • 3
  • 22
  • 29
  • that worked in anaconda promt. I was able to run install in the environment and then running python in anaconda promt ```import numpy``` ```import matplotlib``` and pandas all worked. i guess now my problem with vscode is that it automaticly runns any python program i right in windows powershell in the terminal window, and ```conda activate myenvtest``` wont rtun do to ``` the term 'conda' is not recognized' ``` – Nestly May 09 '20 at 22:58
  • see edit in major post i put a picture there at the end to show you what it is showing me. – Nestly May 09 '20 at 23:08
  • are you running conda in a virtual environment. – Natsfan May 09 '20 at 23:09
  • I think so. i made the conda environment and the python interperter selected in vscode is "python 3.8.2 64-bit ('myenvtest":conda)" – Nestly May 09 '20 at 23:13
  • So I tried a mix of things now. but one thing that seems to be in the right direction is what @gert mentioned about adding path in environmental variables. Now i can run ```conda create -n myenv``` or other conda cmds in windows powershell and cmd but still does not want to work in vscode. before i did that it ```conda``` was not working in cmd – Nestly May 09 '20 at 23:29
0

I reproduced your problem and found the reason (probably is).

The key is that the py file needs to run in a specific environment. Obviously, your conda environment is not activated.VSCode's official python language extension does not currently support automatic activation of conda environment from Powershell. If you use conda, you need to switch your terminal to CMD.

About automatic activation of virtual environments

At first, config Conda Path conda path

When you use conda virtual environment as python interpreter, VSCode will automatlly active it after you open a new terminal window.Just like this(CMD Terminal): enter image description here So you can see the prompt starts with "(test_new)",it means that conda env is ready.

If we change to Powershell terminal,the result may be like this: enter image description here VSCode currently does not support automatic activation of Conda environment from Powershell(In fact, Anaconda supports powershell, you can find it in Start menu for Windows).

alexzshl
  • 902
  • 6
  • 6
  • So I found a way to make it work in both Mac and Windows on VS code. it is a bit of a work around. After conda environment is created in terminal/anaconda powershell and all your python packages are installed. Open the anaconda Navigator app and open VScode from there. Seems to somehow activate the terminal in VScode Only had to do that once then it worked every time after that. with a few hiuckups. but when an error happened i just re tried the same method and it worked. – Nestly Jun 05 '20 at 14:28
  • In fact, I had report a issue, and I got a lot of ways to have powershell automatically activate the conda environment in vscode, but none of them were perfect. The only thing close to perfection should be to install a profile file for the powershell using the conda command: `conda init powershell`. Of course, start vscode from anaconda promt is also a good way. https://github.com/microsoft/vscode-python/issues/11638 – alexzshl Jun 06 '20 at 01:56
0

So What has ended up working for me, both on mac and pc, was after install of anaconda, I created my conda environment. On mac I did this all through Terminal, on PC I used the anaconda cmd or powershell, both work.

Once conda environment is created and your python packages are added, open VS code using the anaconda application. Some how, not sure how, it activates the terminal to work with anaconda. Make sure you choose your anaconda environment as your python interpreter once folder is chosen. Note that I only had to do this once. After that I never had the issue again, yet. So I think this is a good solution for others that maybe having this same problem.

Im no expert, but that is what worked for me. all packages work and my code runs properly. Hope this helps someone else.

Nestly
  • 41
  • 2
  • 6