I am having some major problems importing Pandas and it is driving me crazy.
I installed Pandas from terminal using
pip install pandas
as well as all dependencies.
Now when I try to import pandas I get
ImportError: No module named pandas
This is true in terminal and in Wing.
I have tried changing python path but I am not really sure how to do that. I am a Stats guy not really a developer. My goal ideally is to have Wing point to the python where pandas is located but also to be able to access pandas in terminal as well:
Mac / usr / local / lib / python2.7 / site-packages / Pandas
When asking in terminal
which python >>> /usr/bin/python
Which is running python 2.7.6 and the pandas package is sitting in my python2.7 folder, so I don't know why it's not recognizing it.
Is this a quick fix or a deeper issue in the way I installed pandas, Wing, and python?
You have two different Pythons in your system: Apple's default one in:
/usr/bin/python
and one in:
/usr/local/bin/
If you run:
/usr/local/bin/python34
you will be able to import the packages installed with pip34:
pip34 install pandas
If you want to permanently use the new Python, then make an alias or change the search order in our PATH env variable so that /usr/local/bin/ is searched before /usr/bin.
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 am having issues with importing the pandas library into my project on VS Code. I have pandas installed on my machine and my machine is currently running python 3.9.12. I tried changing the interpreter in my IDE from 3.10.64 to 3.9.12 but I cannot find the path to it. I was wondering if there was anything else I should try/do to help fix this issue. Thank you!
Make sure you set the path using your environment variables as shown here
Then, using pip, just simply do !pip install pandas in order to have the pandas path set correctly automatically. I understand you may have installed it manually but this may help.
I currently need to use pandas with Thonny, because pandas is installed locally there, but all my coding is in visual studio and i want to be able to use pandas in the editor i like. I have this error while trying to run a code for pandas.
No module named 'pandas'
File "/Users/myname/Desktop/Ambassador.py", line 1, in <module>
import pandas as pd
Even tho i have already installed it, i've made several attempts with pip, conda, i installed wheel and everything suggested.
Anyone knows why is this ?
for additional info i have a Mac
Check python version where pandas is installed and use same version to run your code
It could be cause you have installed pandas at the different virtual environment. I guess, you should try to check and change environment (⇧⌘P) in visual studio especially if you are using things like conda.
Also, check it for info about environments: https://code.visualstudio.com/docs/python/environments
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))
I am struggling with installing opencv for python 2.7.11 on OSX for almost three days now.
After some failures, I accomplished a first success by following the instructions here. So my basic setup is python 2.7.11 running from ~/.virtualenvs/cv_env/bin/python and I have a cv2.so located in ~/.virtualenvs/cv/lib/python2.7/site-packages/.
So good so far. Using source ~/.virtualenvs/cv_env/bin/activate, I can activate the virtualenv and than use import cv2. For some reasons, this does not work always. From time to time, I have to deactivate first and than reactivate (any guesses?).
Next, I wanted to use opencv in PyCharm. Under "preferences - Project interpreter", I selected the virtualenv interpreter and this is also working. I can import other moduals like numpy and pandas (previously installed into the vortualenv using pip). But for some reasons, I am not able to import opencv (import cv2). It always gives me
ImportError: No module named cv2
So my question is, why I am able to import opencv in terminal (at least sometimes) but not in PyCharm. Any ideas are welcomed.
Best,
Fabian
Your cv2.so located in a different directory. And you activating from another directory. I mean cv_env and cv.