So I am new in Python. I downloaded Anaconda (and Homebrew) and using Terminal on my Mac I confirmed that, say, Numpy is installed:
pip install numpy
to get as a result
Requirement already satisfied: numpy in ./anaconda/lib/python3.6/site-packages
Then opening Python within Terminal I can indeed import the package. The story is not the same once I open IDLE. So when I open it and try to import, say, Numpy I get the following message:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
I guess there is something I do not know, I assume it is a common rookie problem. Can you help me with this?
Since your IDLE is using Python 3 you should use
pip3 install numpy
to get it installed for the correct Python. If you want to use your default installed Python (2.x) instead, IDLE is located at /usr/bin/. From your terminal you can open it with
/usr/bin/idle
this should have the numpy you installed with pip install numpy
See when you are working with Anaconda it creates its own virtual environment.
Now, If you are new in python on MAC then you might not familiar with Virtual Environment.
I suggest you to download python from the official website www.python.org
or Click here to directly download Python 3.6.2 on your MAC.
Then, Download Pycharm(The Best IDE for Python)
Download the community version for beginner and it is also free.
Pycharm Community Version -> Click Here
And do whatever you want and also must read about Virtualenv
According to me, Pycharm is better then Anaconda's Jupyter Notebook
Wish you good luck and show your creativity in python ! !
Erm... well it appears as though - since you have 2 versions on your computer - that it might be because it imported it into the other file path directory. Now, I've never worked with Mac, but I think if you just specify which pip you want to download from, it might work.
For example, on Windows:
C:\Python34\Scripts\pip.exe install numpy
or if you were doing it for 3.6, you would follow the path to the folder, find pip and install. So, I suggest to install the normal IDLE before any other platform built upon it just because it is easier to import modules and is not as bad to break like yours has.
There can be two issues -
You are using python 2 as a kernel for IDLE, since numpy is installed for python3.6. This will raise an error.
The issue is with anaconda's configuration with IDLE. Anaconda installs numpy in ./anaconda/lib/python3.6/site-packages. If IDLE uses systems default python instead of anaconda's. You will not be able to import numpy.
You can try running this snippet -
import sys
print('\n'.join(sys.path))
to track the location of python exactly.
One of the workaround that I can think of is -
create a virtual environment using anaconda. Something like
conda create -n py352 python=3.5.2 anaconda
and then fire IDLE from your terminal. Though I am not entirely sure, if this works for mac. If it doesn't, let me know the output of -
import sys
print('\n'.join(sys.path))
Related
I have installed Python 3.10.6 and Pycharm community edition.
Everything was working until I tried to use numpy.
pip3 install numpy
import numpy as np
This is the error message:
pip3 install numpy
^^^^^^^
SyntaxError: invalid syntax
I also have tried to use pip install numpy and pip2 install numpy and pip3 install numpy scipy, but same error. Reinstalling both python and pycharm didn't help.
Ah, I understand your problem more specifically now. I also use PyCharm, and this same problem happened to me. It was very frustrating, and took me lots of reading to fix it.
PyCharm and other IDEs (integrated development environment) have something called 'run configurations' attached to each file you are working on. These run configurations basically specify which directory on the hard drive the file will use to read and execute your commands. The directory will contain the libraries you need to run your code.
They use these configurations to make it easy to quickly choose which directory (and which libraries) you want a certain file to use. You must specify these configurations in PyCharm for your specific file to run using Numpy. The great thing about PyCharm is that you can actually specify libraries you want to use within the IDE itself (and bypass having to specify a computer-native directory).
Here's How
Go to PyCharm Preferences
Expand the arrow that says 'Project: (your project name)'
Click on 'Python Interpreter'
Click the small '+' symbol
Type in 'numpy' to search for the library (package)
Click install package
Now try to run your file and it should be good to go!
Note that you must do this for each package you wish to use when accessing your file, and as you advance your programming knowledge it will be necessary to learn how to specify the directory you want PyCharm to run the Python Interpreter from. Since you are only using one library though, I think this solution should be fine for the time being.
You should install numpy with that command in your bash/zsh shell.
pip3 install numpy
the python script can then import it.
to test, run pip3 install numpy
then,
python to open a python shell.
and then you'll see
>>>
Type import numpy as np and be sure it imports. It should now.
It can be maddeningly confusing when first starting out with python and trying to figure out how to download libraries. Here are a few critical things I wish I understood before starting my Python journey, as well as the answer to your question.
Python is the language, and the files that support its functionality are located on the hard drive.
Libraries (like Numpy) can be thought of almost as interpreters (note that we are not using the computer definition of 'interpreter') and are stored alongside the Python files on the hard drive. They give Python more flexibility in terms of what it is able to do by increasing what commands Python is able to understand.
Once a library is downloaded, it must be imported to your Python script before you start writing library-specific commands. Importing a library tells Python: "Hey, I'm going to be writing some commands that you haven't seen before, but here is the library with the commands and what they want you to do in a way that you understand."
'pip' is Python's installer for these libraries.
Ex) I have a csv file that I want to read. I learn that Pandas has a csv reader function:
pandas.read_csv()
If I were to type this function in a script, Python would have no idea what I meant. But if I were to download Pandas, then import it into my script, Python would understand exactly what I'm saying.
How to Download Numpy
Assuming you are on Windows, open the terminal (command prompt) and run the command:
py -m pip install numpy
If you don't already have it, the terminal should have a few lines run and should end with something like 'numpy installed successfully'.
You can check to see if you have it by running the following command in your terminal:
py -m pip list
This command provides you with a list of all the downloaded libraries. You can check among them to make sure Numpy is downloaded.
Importing Libraries
Once you've downloaded the libraries you need, you need to import them into your script (the Python file where you are writing your code) in order for it to run properly. This is accomplished using the import command. One important thing to note is that you can import libraries and assign them a nickname using the as modifier.
Ex) Back to that csv file I want to read. I don't want to type 'pandas' in front of all the Pandas commands, so when I import it into the script I abbreviate it as 'pd':
import pandas as pd
pd.read_csv()
See the difference?
TL;DR for Your Scenario
Go to the terminal, and use the py -m pip list command to check if you have Numpy downloaded. If not, run the py -m pip install numpy command. Then go to your script with your actual python code, and import numpy with the import numpy command. Common Python practice is to import numpy as np, FYI.
Hope this clears things up.
It may say that you need to upgrade pip, which is fine, and it should give you a command to run that will upgrade pip to the newest version.
I've been trying to install simple packages for python such as numpy and pandas, and while I am able to install the package manually from the command line using pip as soon as I try to import it PyCharm can't find it. I have manually configured the path, with no luck.
PyCharm can also not find the packages I want to install itself and produces a connection timeout, this could be because I am working from a secure network.
Still pretty new to python and software dev in general, I apologise in advance if I'm being stupid, but no other similar threads are solving the issue.
I am operating on windows 7. The error I get is:
C:\Users\james.alexander\PycharmProjects\Test.py\venv1\Scripts\python.exe C:/Users/james.alexander/PycharmProjects/Test.py/testfile.py
Traceback (most recent call last):
File "C:/Users/james.alexander/PycharmProjects/Test.py/testfile.py", line 1, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
I think PyCharm use a virtualenv, installing via pip won't work since it will install the package in your local env.
Check https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html
Try This:
Step 1: Ctrl+Alt+s
Step 2: Look for Project Interpreter Under Project
Step 3: Look for + In the right corner of prompt box.
Step 4: Search and select your required package. And Install button is just below.
First check the Project Interpreter in your pycharm.
File > Settings >Project >Project Interpreter
Check the version of python there (python2 or python3)
And check if you are using any virtual environment
And see what version python you have installed
For python3 you may have to use "pip3 install pkgname"
For python2 use "pip install pkgname"
In PyCharm, you can install modules from within the app. Simply access your preferences, then under 'Project Interpreter', you will find a table containing your installed modules. To install a new one, click the + symbol on the bottom - left, then search for the appropriate package, e.g. numpy. Once you've found it, simply click 'Install Package'. The module is now installed and ready to use!
EDIT: I've just seen that someone else has already posted this answer, sorry about that :)
Brand new to Python (typically program in MSDN C#) and I'm trying to make use of the matplotlib to generate some graphics from .csv files
I've downloaded & installed Python as well as Anaconda onto my Windows 10 machine, the versions are Python 3.5.2 and Anaconda 4.1.1
I open up the Python "notepad" interface and do
import matplotlib.pyplot as plt
plt.plot([1,2,3],[3,2,1])
plt.show()
but when I run the code I get the error:
ImportError: No module named 'matplotlib'
I've looked at some other posts for this but they all seem to be in regard to Mac OSX or Linux. Some have pointed to multiple installs of matplotlib, but I haven't turned up such a situation so far. What might be causing this, or how can I troubleshoot it?
**Edit:
The path returned to me from the import sys recommended in the comments gave me this response
['C:\Users\a.watts.ISAM-NA\Desktop',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\python35.zip',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\DLLs',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\lib',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\lib\site-packages',
'C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\lib\site-packages\setuptools-26.1.1-py3.5.egg']
You essentially have 2 versions of python on your system - the standard one you downloaded and the one that ships with Anaconda. When you are running code in the IDLE you are using the standard version (in C:\Users\a.watts.ISAM-NA\AppData\Local\Programs\Python\Python35-32\python.exe) where matplotlib isn't installed which is why you are getting the error.
You need to use the Anaconda version (C:\Users\a.watts.ISAM-NA\AppData\Local\continuum\anaconda3\python.exe) that comes with the scientific stuff already setup. It looks like your system is using this one from the cmd so if you run scripts from there it should use the Anaconda version. If you want to use something more interactive you can also use spyder - the Anaconda version of the IDLE - or run jupyter notebook from cmd to get a browser based platform for interactive development
From IDLE, I tried to run a script with a newly installed scrapy 1.0.3.
I'm using a script from a friend whom it worked for (but on Windows, I'm on a Mac).
From the import of scrapy on the first line, I get this error when running the program:
ImportError: No module named twisted.persisted.styles
The whole script, if it's helpful, points to this:
Traceback (most recent call last):
File "/Users/eliasfong/tutorial/tutorial/spiders/medspider.py", line 1, in <module>
import scrapy
File "/Library/Python/2.7/site-packages/scrapy/__init__.py", line 27, in <module>
from . import _monkeypatches
File "/Library/Python/2.7/site-packages/scrapy/_monkeypatches.py", line 20, in <module>
import twisted.persisted.styles # NOQA
ImportError: No module named twisted.persisted.styles
Any suggestions on how to tackle this problem?
Just try to force the update of twisted :
pip install twisted --upgrade
That works for me with python3.4 and Scrapy==1.1.0rc1
Either twisted is installed on your mac (I highly doubt it since it's not a standard library) and for whatever reason the IDE (i'm assuming that's what you mean since you typed "idle") or the terminal you are in doesn't have your updated environment variables, meaning it doesn't understand where your default python libraries are (again I highly doubt it), or you simple do not have twisted installed on your mac. If it's not installed you have a couple of options:
The easiest way to install a python package is through pip.
If that not an option you can try homebrew which is another package manager for macs. It offers an easy way to install packages correctly.
If that still is not an option for you or you simply don't want to attempt that you can download twisted directly from here (the .bz2 since you're on a mac), click on it and it should unzip it for you. Then just run setup.py and it should install it in the correct location on your mac.
If that still doesn't work and you have decent knowledge of unix. Use the "locate" command on the terminal and find out where your dist-packages directory is and put the source for twisted in there directly and then attempt to import twisted in your IDE or in the python interpreter to verify that it is installed.
note: If you're still having problems after it is installed trying restarting your IDE or messing with some setting to make sure your IDE has the right environment and python path. Hope that helps!
It could be related to having installed Python without bzip2. I had the same error and this helped me, see the accepted answer here:
Installing Twisted through pip broken on one server
Had this exact thing on FreeBSD. Solution (as root/sudo):
chmod -R go+rX /usr/local/lib/python2.7/site-packages
Some directory permissions weren't set up right on install.
I'm trying to start using the pygame module but I can't get it to run. I'm using Mountain Lion with Python 2.7 and MacPorts, but I also installed some science and math modules using Anaconda before I ever discovered and started using MacPorts. Note that my MacPorts was just updated before I started any of the following. I initially tried to just use:
sudo port install py27-game
which looked like it worked and set everything up without a problem. But, when I go into the Python interpreter from the command line and type:
import pygame
I get the response:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pygame
So then I went in and did:
import sys
print sys.path
which gave:
['', '/Users/trav/anaconda/lib/python27.zip', '/Users/trav/anaconda/lib/python2.7',
'/Users/trav/anaconda/lib/python2.7/plat-darwin',
'/Users/trav/anaconda/lib/python2.7/plat-mac',
'/Users/trav/anaconda/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/trav/anaconda/lib/python2.7/lib-tk', '/Users/trav/anaconda/lib/python2.7/lib-
old', '/Users/trav/anaconda/lib/python2.7/lib-dynload',
'/Users/trav/anaconda/lib/python2.7/site-packages',
'/Users/trav/anaconda/lib/python2.7/site-packages/PIL',
'/Users/trav/anaconda/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
So, I'm guessing that because I used the Anaconda set up when I initially put the NumPy, SciPy & Matplotlib setup on here that this has caused MacPorts to clash with this somehow in the path.
Should I just remove the Ananconda package? If so, how can I go about removing these dependancies when I do that?
Ok, so I figured out the problem, and it was my path. I went in and removed the anaconda package with:
rm -r ~/anaconda
Then, I used macports to basically reinstall the whole scipy stack with:
sudo port install py27-wxpython py27-numpy py27-matplotlib py27-scipy py27-ipython
This took some time to compile, and when it was finished I went in on the command line and used:
sudo port select --set python python27
After that I opened my interpreter and imported all my scientific computing needs without a problem and pygame, which I had installed earlier with macports. I hope this helps someone else in the future. ALSO: when you remove packages like anaconda, make sure and close your terminal and then re-open it or it will still try to use the dependancies of anaconda, which are no longer there. I had macports set up already so after removing anaconda the macports path became the default.
One last edit. In order to get all of this to run correctly and allow me to run the scripts from within emacs as well with all the imported modules I had to switch to aquamacs from my normal emacs editor in order for the correct path to be used from within the emacs environment , or I could have just run emacs from the terminal with /Application/Emacs.app/Contents/MacOS/Emacs.