PyCharm on Mac osx ModuleNotFoundError: No module named - python

idle PyCharm
os Mac osx
python 2.7
python3
Coding with PyCharm this error showed up..
ModuleNotFoundError: No module named 'numpy', same with matplotlib and
other ones.
Any solution?
already changed interpreter to python3
it is working on bash terminal both python 2.7 and python3
import datetime as dt
import numpy
import matplotlib as plt
from matpololib import style
import pandas as pd
import pandas_datareader.data as web
style.use('ggplot')

assume that you're using macOS with pyenv for different python versions.
Take the matplotlib stacked bar graph bar_stacked.py as the example (you could get the .py from https://matplotlib.org/gallery/lines_bars_and_markers/bar_stacked.html#sphx-glr-gallery-lines-bars-and-markers-bar-stacked-py).
short answer: quickly configure pyenv python as this snapshot shows:
quick config steps
details
open the bar_stacked.py with PyCharm;
check your python environment settings:
2.1 check your python version with your terminal:
ABC-MacBook-Pro:~ ABC$ which python
/Users/ABC/.pyenv/shims/python
2.2 check your pyenv installed pythons
ABC-MacBook-Pro:~ ABC$ pyenv versions
system
* 3.7.0 (set by /Users/ABC/.python-version)
2.3 confirm the python version you're using, here is Python 3.7.0
ABC-MacBook-Pro:~ ABC$ python --version
Python 3.7.0
3.configure the PyCharm interpreter as follows:
3.1 PyCharm -> Preferences -> Project -> Project Interpreter, you'll see a icon like a gear, click it and choose Add...
3.2 choose Virtualenv Environment on the left
3.3 choose Existing environment on the right, in the pop-up, locate the python i'm using as checked in step 2.1, here is /Users/ABC/.pyenv/shims/python
3.4 you can also check the box Make available to all projects if you want
finally, this example in PyCharm runs like this:
running

Have you noticed the information of the popup when your mouse is hovering over the red waved underline? Just click "install matplotlib". Then it's ok.

Related

VSCode - import errors and interpreter errors

I have several imports in my current code:
from flask import Flask
import datetime as dt
import numpy as np
import pandas as pd
When I run this in VSCODE I am getting this error:
However, running this in jupyter notebook has no problems. When I looked online it said to use python interpreter but when I go to do that I get this error:
And another error:
Anaconda prompt says modules/packages are installed but when I run pip install in default windows terminal it says pip has no module:
Delete and reinstall Python extensions according to the same problem on github.
The second problem is related to the location of your Python interpreter. You need to choose the correct interpreter. I still recommend using one Python version in one environment. You can use other Python versions in virtual environment, so it won't lead to confusion.

Python pyfuglet modules installation

there is some modules that when im trying to install from pypI, like pyfiglet or Django framework, i'll saw some errors. how can i fix it? my python versio is 3.9.4
For installing and checking the python module "pyfiglet" in VS Code, please refer to the following:
Please make sure that python is available, and select the required python environment in the lower left corner of VS Code.
Please use the shortcut key Ctrl+Shift+` to open a new VS Code terminal, and check whether the python used by the terminal is the same as the lower left corner of VS Code: ( python --version )
Then install the module "pyfiglet": (pip install pyfiglet)
Check the installation of this module: (pip show pyfiglet)
Run:
Reference: Getting Started with Python in VS Code and Python environments in VS Code.

VSCode issue with Python versions and environments from Jupyter Notebooks

Issue: I am having issues with the environment and version of Python not matching the settings in VSCode, and causing issues with the packages I am trying to use in Jupyter notebooks. I am using a Windows 10 machine with Python 3.9.1 installed (including older versions), with Visual Studio Code 1.52.1 . Short summary - I install a package using pip. My guess is that it associates with the latest version of Python. I set up an interpreter in VS Code for that version of python, and try to import the package. The package is not found. If I call sys.version from the Jupyter notebook, I see that a default version of Python is running (3.8.5). The simple notebook throws an error because it cannot find that package that I installed with pip.
Screenshot that shows the associations:
This error is reproducible with only the
import pygmt
command in the notebook, i.e. it does not depend on the other packages imported.
Question: How can I control the version that Jupyter notebooks will run in VSCode if changing the interpreter doesn't work? Every other issue like this that I have found was due to not choosing the correct interpreter. In this case, I have. Why is it not running that version of the interpreter?
Thank you.
Edit
This edit is in response to #Jill Cheng's answer (see my comment below for tl;dr). I can change the interpreter (lower left corner of VSCode) to match the default that VSCode wants to run. No problem.
But there still are problems.
I cannot install pygmt, the targeted package, into this version of Python (see the command in in the terminal).
I cannot make VSCode operate in the opposite direction - i.e. I can never get it to run Python 3.9.1 even when that is the interpreter I chose.
I have reinstalled VSCode now several times, and Python 3.8.5 seems to always be the default. To me, the easiest solution seems to be changing the default Python version of VSCode - in fact, isn't that the point of selecting an interpreter? It is more nebulous to me why I cannot install pygmt into Python 3.8.5, and I don't know if it is beneficial to have multiple different versions of Python all with different packages (or is this just what Python users deal with daily?).
In VS Code, the Python kernel (Python environment) used by Jupyter notebook can be independent of the Python environment we selected in VS Code (shown in the lower left corner of VS Code).
As the output in the screenshot shows, the Python kernel of Jupyter you are using is "Python3.8.5", but the module "pygmt" is not installed in this environment. (Jupyter uses the last selected Python environment by default.)
Solution: Click the Python kernel on the upper right in the Jupyter notebook, and select the python environment where the module "pygmt" has been installed. In addition, it is recommended that you reopen the jupyter file after switching Jupyter's Python kernel so that it can reload the new python kernel.
Reference: Jupyter notebooks in VS Code.

Stop Spyder from importing modules like `numpy`, `pandas`, etc

When I start Spyder, it automatically imports pandas and numpy. Is it possible to have Spyder ignore these modules?
I see these are imported in multiple Spyderlib files. For example, pandas gets imported in spyderlib/widgets/importwizard.py, spyderlib/baseconfig.py, etc.
(I'm trying to debug something in pandas and I'd like to import it for the first time in a debugging session in Spyder)
(Spyder dev here) This is not possible. If Pandas is installed on the same Python installation where Spyder is, then Spyder will import Pandas to: a) report to its users the minimal version needed to view DataFrames in the Variable Explorer and b) import csv files as DataFrames.
The only solution I can suggest you is this:
Create a new virtualenv or conda environment
Install there Spyder and its dependencies, but not Pandas. Spyder dependencies can be checked under the menu Help > Optional dependencies
Start your virtualenv/conda env Spyder
Go to
Tools > Preferences > Console > Advanced Settings > Python executable
select the option Use the following Python interpreter and write (or select) there the path to the interpreter where you have Pandas installed (e.g. /usr/bin/python)
Start a new Python/IPython console and import pandas there.
Might be a little late, but I ran into this issue when I updated Spyder to 5.3.3 where it was automatically loading numpy and matplotlib. The solution that worked for me:
Navigate to Tools > Preferences > IPython console > Graphics
Uncheck "Automatically load Pylab and NumPy modules"
Start a new IPython console.
Hope this helps!

python newbie: importing numpy module in eclipse

I'm trying to run a simple python program in eclipse. This is the first time that I'm importting any module.
So I downloaded: numpy and pylab (I'm using a mac) restarted my eclipse and the red line below the
import numpy
import pylab
disappeared so I understood that the reference to that module is ok.
Problem is that I still see red line below the code and wonder why? I have to stress out that I believe numpy was already 'pre-installed' I just upgraded the version (using 1.5.1-py2.7).
Can anyone tell what should I do to run this code?
my interpreter setting on eclipse:
If you are using PyDev, you should first have to go to Preferences, then Pydev, then Interpreter Python and then Libraries to add NumPy.
Else, verify that you have NumPy installed, from the interpreter, just call from numpy import *
Edit:
Also check you already have Matplotlib installed, the error you are getting on the console points to that being the cause, you can download Matplotlib here.
I recently installed Anaconda3 and just started learning how to use Pandas and I wanted to be able to work with Pandas in Eclipse as well.
I first tried adding a reference to the site-libraries at:
C:\Anaconda3\Lib\site-packages
And it seemed to work by allowing me to import numpy and pandas. However, since I had just used conda to update everything, my Python34 interpreter seemed to fail when I tried running some code and numpy was looking for my Python35 installation. For some reason this was located at:
C:\Users\myname\AppData\Local\Programs\Python\Python35-32
However, Anacondas installed another version somewhere else. By going into:
Windows > Preferences > PyDev > Interpreters > Python Interpreter
and clicking on Quick Auto-Config it found my Anacondas version of Python35 and then I just had to figure out how to make my current project use the Python35 interpreter.
Hint: To do this, you need to go into the Project properties by opening the project and choosing File > Properties or right-click the project to choose Properties.
Simply removed the old numpy and installed version 6. located here
Another way to circumvent this problem is to use pip install numpy check how to install pip

Categories

Resources