Problems with anaconda and VScode

Viewed 3534

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

4 Answers

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.

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

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.

Related