So, I have been working in the jupyter lab and I have created a code that uses numpy.
In the jupyter I had no problem using numpy. I installed the library, and I used the command import to use such module.
However, I had to save my code as a py file. When I try to run my py file using my cmd, I receive this message:
ModuleNotFoundError: No module named 'numpy'
If i type in my cmd "pip show numpy" it shows me that I have numpy installed.
I also have tried to add to my code a line with:
pip install numpy
pip3 install numpy
python -m pip install numpy
python3 -m pip install numpy
conda install -c anaconda numpy
conda install numpy
and for every option I received a syntax error:
SyntaxError: invalid syntax
Can someone help me?
As mentioned in the comments section, it seems like your Python script (.py) was not using the correct environment. Please ensure that correct virtual environment is selected by doing conda activate name_of_the_kernel, where name_of_the_kernel should match the kernel used in your Jupyter notebook.
I am currently practicing matplotlib. This is the first example I practice.
#!/usr/bin/python
import matplotlib.pyplot as plt
radius = [1.0, 2.0, 3.0, 4.0]
area = [3.14159, 12.56636, 28.27431, 50.26544]
plt.plot(radius, area)
plt.show()
When I run this script with python ./plot_test.py, it shows plot correctly. However, I run it by itself, ./plot_test.py, it throws the followings:
Traceback (most recent call last):
File "./plot_test.py", line 3, in <module>
import matplotlib.pyplot as plt
ImportError: No module named matplotlib.pyplot
Does python look for matplotlib in different locations?
The environment is:
Mac OS X 10.8.4 64bit
built-in python 2.7
numpy, scipy, matplotlib is installed with:
sudo port install py27-numpy py27-scipy py27-matplotlib \
py27-ipython +notebook py27-pandas py27-sympy py27-nose
pip will make your life easy!
Step 1: Install pip - Check if you have pip already simply by writing pip in the python console. If you don't have pip, get a python script called get-pip.py , via here: https://pip.pypa.io/en/latest/installing.html or directly here: https://bootstrap.pypa.io/get-pip.py (You may have to use Save As ..)
Step 2: Take note of where the file got saved and cd the directory from command prompt. Run the get-pip.py script to install pip.
You can write in cmd this line within quotes: "python .\get-pip.py"
Step 3: Now in cmd type: pip install matplotlib
And you should be through.
You have two pythons installed on your machine, one is the standard python that comes with Mac OSX and the second is the one you installed with ports (this is the one that has matplotlib installed in its library, the one that comes with macosx does not).
/usr/bin/python
Is the standard mac python and since it doesn't have matplotlib you should always start your script with the one installed with ports.
If python your_script.py works then change the #! to:
#!/usr/bin/env python
Or put the full path to the python interpreter that has the matplotlib installed in its library.
If you are using Python 2, just run
sudo apt-get install python-matplotlib
The best way to get matplotlib is :
pip install matplotlib
cause the previous way may give you a old version of matplotlib
This worked for me, inspired by Sheetal Kaul
pip uninstall matplotlib
python3 -m pip install matplotlib
I knew it installed in the wrong place when this worked:
python2.7
import matplotlib
use pip3 install matplotlib to install matlplot lib.
By default, pip will install those package for 2.7 as it the native one.
using pip3 makes it specific for python 3, and make sure you have only one version of python 3
First check the version of Python
For python2:
sudo apt-get install python-matplotlib
For python3:
sudo apt-get install python3-matplotlib
If you mismatch the Matplotlib installation and the Python version you will get the no-module-error because no module for that version exits.
If you using Anaconda3
Just put
conda install -c conda-forge matplotlib
You can install the matplotlib package in python 3 by doing this
python3 -m pip install matplotlib --user
It's working for me.
If you have pip installed and Python configured to the path variables, Just run this command in the terminal.
pip install matplotlib
So I used python3 -m pip install matplotlib then import matplotlib.pyplot as plt and it worked.
I had a similar problem, using pip3 and all these things worked for installing matplotlib but not pyplot. This solved it for me:
import matplotlib as plt
from matplotlib import pyplot as pllt
I had a similar issue that I resolved and here is my issue:
I set everything up on python3 but I was using python to call my file for example:
I was typing "python mnist.py" ...since I have everything on python3 it was thinking I was trying to use python 2.7
The correction:
"python3 mnist.py" - the 3 made all the difference
I'm by no means an expert in python or pip, but there is definitely a difference between pip and pip3 (pip is tied to python 2.7) (pip3 is tied to python 3.6)
so when installing for 2.7 do: pip install
when installing for 3.6 do: pip3 install
and when running your code for 2.7 do: python
when running your code for 3.6 do: python3
I hope this helps someone!
I bashed my head on this for hours until I thought about checking my .bash_profile. I didn't have a path listed for python3 so I added the following code:
# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH
And then re-installed matplotlib with sudo pip3 install matplotlib. All is working beautifully now.
The file permissions on my virtual environment directory and my project directory were not correct and, thus, would not allow me to install the proper packages. I upadated them by running:
sudo chown user:user -R [project folder]
sudo chown user:user -R [environment folder]
In the above your should use your own usernames in place of "user". The -R recurses through all subfolders and files.
Working on the django project and faced same problem,
This is what I did.
Check if you have matplotlib already simply by writing pip show matplotlib in the python terminal.
If dont, run the command pip install matplotlib.(make sure you have pip downloaded)
Run the command pip show matplotlib again.
If the library is successfully downloaded, you can run your project (py manage.py runserver)
If you are in a venv, don't install these libs within the venv.
Installing them from outside helped me.
For me, with PyCharm and Python 3.8 on WIN10 environment, the solution was inside the IDE of PyCharm itself.
From the IDE and Project Setting (the gear on the top right corner), I've chosen Settings -->Project:Python-->Python Interpreter and you can see all the packages installed (and matplotlib wasn't present in the Package list).
Click on the + icon in the upper left corner for Package installation, and next will be opening a new window with the available packages for PyCharm. In the find box was sufficient to type matplotlib, select it and click on Install Package.
Here a screen-shot of PyCharm environment (as requested on 21/08/2021) :
If you experienced this error ModuleNotFoundError: No module named 'matplotlib' while using venv and running pytest like me, you can check by comparing the path type pytest shows and the path type python shows. In my case pytest wasn't under venv/bin directory like python, I installed pytest by pip and reactivated venv. See https://stackoverflow.com/a/54597424/3622415
I have installed matplotlib using both pip and conda but experienced this error ModuleNotFoundError: No module named 'matplotlib' .
I fix by following.
Because It might have an old version of Jupyter notebook, so i try this command to install it in the current kernel.
import sys
!{sys.executable} -m pip install seaborn
In the New Jupyter version (2019) can be installed simply as:
%pip install matplotlib
I solved by conda. Once installed miniconda or anaconda, type
conda install matplotlib
then, when prompted, type
y
in sublime text you can set
"cmd": ["python3", "-u", "$file"]
in tools > buildsystem > new build system
it worked for me
I tried to install Numpy library with VisualStudio Code (VS Code) used the terminal and official website for instructions
Even though I followed each step I keep getting "This is the wrong setup.py file to run error"
I tried to update every element to not get an error, deleted and installed NumPy files in the directories which are in site-packages, and my anaconda files (i use jupyter as well but I need to implement this on my VSCode editor).
I also tried to get in the NumPy file and tried
pip install.
python setup.py build_ext --inplace
I used this site's instructions as well to install NumPy:
here I tried :
python -m pip install --user numpy
but keep getting the same error. What am I doing wrong?
In the screenshot you provided, I noticed that the installed module "numpy" exists in the "python3.7" folder, not in the "python3.8" you are currently using.
This is where my environment and numpy are located:
It is recommended that you use the shortcut key Ctrl+Shift+` to open a new terminal, VSCode will automatically enter the current environment, and then you can use "pip install numpy" to install numpy into "python3.8".
Or you can switch the environment directly to the python3.7 environment that includes numpy.
If it still doesn't work, you can uninstall numpy and reinstall it. ("pip uninstall numpy", "pip install numpy")
Since we are using pip to install the module numpy, we can use "pip --version" to check the currently used pip version, the module is installed in this environment:
I usually work with pyCharm in which it is really easy to manage different python versions. (I use High Sierra, Mac)
In my settings I can see all of them and addon packages:
To be honest, I don't remember how I installed all of these, a bunch of them through brew though.
Now I am trying to run a Jupyter Notebook in python 3, it's using the python 3 kernel, but I am missing acp package.
So I pip install acp. ok.
The Notebook doesn't see the package. This makes me think it's not installing in the correct python version.
pip3 is not recognised as a command.
brew install python3 -> asks to update python 2 to version 3 (which I don't want).
I am quite confused now as how to intall the packages...
It may be installing with python executable your jupyter is not using.
you can directly install in jupyter shell by allowing python executable your jupyter is using:
import sys
!{sys.executable} -m pip install acp
To check your Python executables, you may open a terminal and type
touch ~/.bash_profile; open ~/.bash_profile
Then you will know which path your python exists in. Make sure to select the same path when you configure your project interpreter. Then you may like to upgrade your pip by typing in a terminal:
$ sudo pip install --upgrade pip
$ alias python=python3
$ python --version
Make sure python version is >3, after which that you may try:
pip3 install acp
I'm trying to teach myself python, and I feel out of my depth. To start, I am working on a mac which already comes with python 2.7 installed.
I installed python 3.6 recently and have been using it to teach myself the basics. I'd like to eventually learn how to produce mathematical plots in python, and I know I will need the matplotlib package to do that.
Following some advice online, I was told that python3 already comes with pip installed, which is what I thought I should use to install matplotlib. The advice said I should type the following into the mac terminal:
python3.6 -m pip install matplotlib
I typed this, and it seemed like the package was installing, but I ended up getting some sort of error code that said:
Command "python setup.py egg_info" failed with error code 1 in [folder].
I tried opening IDLE and typing "import matplotlib", but I got the error: "no module named matplotlib". I also tried typing "import matplotlib.pyplot as plt", but I got the same error.
Based on further research and this youtube video, I've decided to just install miniconda in order to have access to the matplotlib package.
The problem is, I'm not sure if I should somehow be uninstalling whatever was installed when I ran the code above to install matplotlib. I've actually run that line of code 3 or 4 times. Should I remove anything before installing miniconda? Also, I am running python 3.6, while miniconda is listed on the website as being for python 3.5. Does this mean it won't work for my version of python?
Running pip like that would install packages system-wide. I'm guessing it's failing because you're not running as root (i.e. the administrator user). But wait! Don't try again as root! Instead of installing packages, do it in a virtual environment. First create it:
virtualenv myenv
This creates a directory called myenv with a bunch of stuff in it (so make note of where you run this command). Whenever you want to use the virtual environment (like straight away!) you first need to activate it:
. myenv/bin/activate
Don't miss out that dot (followed by a space) at the beginning! As the other answer says, the first thing you should do in it is upgrade pip:
pip install --upgrade pip
Now you're ready install whatever else you like:
pip install matplotlib
One last note: The virtual environment is tied to a particular Python version. By default it uses the system's Python 2.7 installation, so to use a different one you need to specify it when you create the virtual environment, like this (if that Python version is installed system-wide):
virtualenv -p python3.5 myenv
Or like this (if that Python version is not installed system-wide):
virtualenv -p /path/to/my/installation/of/python3.5 myenv
While the virtual environment is activated, you don't need to specify the particular path/version of Python. Just run it like this:
python
I also encountered many problems during my installation.
It seems that version 2 of matplotlib is not compatible with Python version 3.
Finally, I succeeded by specifying version 3 of matplotlib as follows with the following command:
sudo apt-get install python3-matplotlib
Reference from the Matplotlib website:
https://matplotlib.org/users/installing.html#building-on-linux
Try upgrade setup tools
--upgrade setuptools
or
easy_install -U setuptools
or upgrade pip
pip install --upgrade pip
I ended up downloading anaconda and using the python interpreter that comes with it, as anaconda comes with matplotlib and many other python packages of interest.
the pip command typically is for the Python 2. use pip3 instead to install the libraries in the python 3.X path
This should work
pip3 install matplotlib
The solution that work for me in python 3.6 is the following
py -m pip install matplotlib
Matplotlib files are downloaded in ~/.local/lib/python3.6/site-packages/ and not in /usr/lib/python3.6/ .
Try the command:
sudo cp -r ~/.local/lib/python3.6/site-packages/* /usr/lib/python3.6/