Mangoflavour
Mangoflavour

Reputation: 13

New To Visual Studio Code, having problems with pandas/numpy

I normally use PyCharm for python coding, but just for the heck of it, I tried to use Visual Studio Code today, and I'm having some problems.

So I followed the steps shown in the "Getting Started with Python in VS Code" page, and I copied this to my new python project:

import matplotlib.pyplot as plt
import matplotlib as mpl
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

just to test if it works well, and for some random reason, whenever I run this code through terminal/cmd in VSCode, I get this:

ImportError: Missing required dependencies ['numpy']

BUT when I use the Debug Mode, it seems to work perfectly fine.

similar thing happened when I tried to run my previous projects through VSCode. So I thought maybe it was just the problem with my environment, so I changed it to the one where I have my tools installed, but nope, I still got the error.

I tried uninstalling then installing again and that didn't work as well. I Seriously don't know what's happening right now. Why does it work well in Debug Mode but not in terminal/cmd? does anyone know what to do in this situation?

Thanks!

Upvotes: 1

Views: 6991

Answers (4)

gmolnar
gmolnar

Reputation: 108

I believe it may have something to do with the PATH variables. I just had the same problem in Windows 10 and found out that if I launch VS Code through an anaconda prompt it works just fine, no need to mess around with the PATH variables.

So, instead of opening VS Code through the start menu, just open an anaconda prompt (or anaconda shell), cd to your directory and type code .. That solved my issue.

Upvotes: 0

Yury Wallet
Yury Wallet

Reputation: 1660

In VS Code undee Windows open Terminal -> New Terminal and run

pip3 install pandas

and then

pip3 install matplotlib

While installing pandas, numpy will be installed as well

Upvotes: 0

sevenam
sevenam

Reputation: 622

Got this error and was able to fix it by running conda init in the Python Debug Console terminal, and then closing the terminal before starting a new debug session.

Upvotes: 1

ChaosPredictor
ChaosPredictor

Reputation: 4051

...In Python, packages are how you obtain any number of useful code libraries, typically from PyPI. For this example you use the matplotlib and numpy packages to create a graphical plot as is commonly done with data science. (Note that matplotlib cannot show graphs when running in the Windows Subsystem for Linux as it lacks the necessary UI support.)

Return to the Explorer view (the top-most icon on the left side, which shows files), create a new file called standardplot.py, and paste in the following source code:

import matplotlib.pyplot as plt
import matplotlib as mpl
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

Tip: if you enter the above code by hand, you may find that auto-completions change the names after the as keywords when you press Enter at the end of a line. To avoid this, type a space, then Enter. Next, try running the file in the debugger using the "Python: Current file" configuration as described in the last section. (If you still have "stopOnEntry": true in that configuration, you need to select the run command again to continue.)

Unless you're using an Anaconda distribution or have previously installed the matplotlib package, you should see the message, "ModuleNotFoundError: No module named 'matplotlib'". Such a message indicates that the required package isn't available in your system.

To install the matplotlib package (which also installs numpy as a dependency), stop the debugger and run Terminal: Create New Integrated Terminal from the Command Palette (⌃⇧(Windows, Linux Ctrl+Shift+))). This command opens a command prompt for your selected interpreter. Then enter the following commands as appropriate for your operating system (commands may require elevation if the Python interpreter is installed in a protected area of the file system):

Note: If you are unable to install the package or encounter other problems, please file an issue on GitHub so we can help you investigate.

# Don't use with Anaconda distributions because they include matplotlib already.

# macOS
sudo python3 -m pip install matplotlib

# Windows (may require elevation)
py -3 -m pip install matplotlib

# Linux (Debian)
sudo apt-get install python3-tk
python -m pip install matplotlib

from: https://code.visualstudio.com/docs/python/python-tutorial

Upvotes: 1

Related Questions