AttributeError("module 'pandas' has no attribute 'read_csv'") - python

I am new to Python and I have been stuck on a problem for some time now. I recently installed the module pandas and at first, it worked fine. However, for some reason it keeps saying
AttributeError("module 'pandas' has no attribute 'read_csv'").
I have looked all over StackOverflow and the consensus is that there is likely another file in my CWD with the same name but I believe I don't.
Even if I create a new project and call it, for example, Firstproject.py, and immediately import pandas as pd, I get the error.
I would appreciate the help. I can provide more info if required.

Your problem is this:
The command
import pandas as pd
in your case didn't import the genuine pandas module, but some other one - and in that other one the read_csv() function is not defined.
Highly likely you have in your project directory (or in your current directory) a file with the name "pandas.py".
And - highly likely - you called the pd.read_csv() function in it.
Rename this file, and you will be happy again. (Highly likely.)

Your best bet is to type "pandas" in your console, and you will be able to see where your "pandas" name is originated from:
>>> pandas
<module 'pandas' from '/some-path/site-packages/pandas/__init__.py'>

There might be possibility that you are using this name for your script as read_csv.py hence pandas itself confused what to import, if or csv.py then you can rename it to something else like test_csv_read.py.
also remove any files in the path naming read_csv.pyc or csv.pyc .

Here is the solution
when you downloaded python its automatically download 32 you need to delete if you don't have 32 and go download 64 and then problem solved :)

In my case, I had installed module "panda" instead of "pandas". I was getting this error, even when there was no conflicting .py files were present in working folder.
Then I recognized my mistake, and then installed package "pandas and problem got resolved.

Related

Whenever I try to import a python library I always get "ModuleNotFoundError" what do I do?

Whenever I try to import the libraries "beautifulsoup" or "requests" I always get the same error. The error I get looks like this... ModuleNotFoundError: No module named 'bs4'. I already have these libraries installed I just can't seem to figure out what's wrong.
If you look at the image below, I noticed when I use the recommended interpreter I get a problem saying "Import "bs4" could not be resolved from source". However, when I select one of the other Python 3.10.7 interpreters the problem goes away(see second picture). Either way I still get the Module not found error. I was thinking this info might help diagnose the problem I'm having.
I think you need to cross check environmental variable path of python and editor interpreter path
so, may be possible you downloaded that library but it available on another path for that he can't reach out and you get error

AttributeError: module 'pandas' has no attribute '__version__'

Please note, this is not a duplicate question. I'm aware of how to find versions of libraries in python. What's caught my goat is the apparent anomaly when the __version__ command is called in two different ways.
Has anyone faced this before? When I type the following in IDLE, I get the output which specifies the pandas version.
>>>import pandas as pd
>>>print(pd.__version__)
0.22.0
When I put the same two lines of code into a program, save it and run it, I get the following error:
AttributeError: module 'pandas' has no attribute '__version__'
It does not make any sense to me. Appreciate any thoughts on this please.
It sounds like you're not importing the real pandas module when you run your script from a file. When I run your script in a file on my machine it prints the pandas version correctly.
Maybe you have a file called pandas.py or a directory called pandas in your current working directory when you run the script? Or maybe there is a pandas.py or a pandas directory in a directory in your PYTHONPATH environment variable? (Other than the real pandas, that is.) Try running this program and see if the output looks like the proper pandas source file, or something else.
import pandas as pd
print(pd.__file__)

AttributeError: module 'pandas' has no attribute 'read_csv' Python3.5

I have been successfully using pandas.read_csv since long but suddenly it starts giving the error while I try to read a csv file
df = pd.read_csv('file.csv', encoding='utf-8')
The error is
AttributeError: module 'pandas' has no attribute 'read_csv'
I have tried to upgrade pandas but does not work. I tried to search and got this answer but when I search csv.py file in my pandas I didn't find any. So i tried to hover over the pandas.read_csv method which takes me to parsers.py file. But in that file there is no specific method named read_csv but it directed to another parser funtion like this
# parser.py (built-in file in pandas) file has this implementation
read_csv = _make_parser_function('read_csv', sep=',')
read_csv = Appender(_read_csv_doc)(read_csv)
I don't understand how should it start working again ? Any suggestions
I had the same problem when trying to run the following code in Jupyter/ipython.
import pandas as pd
df = pd.read_csv("weather_data.csv")
df
I realized I had a file named pandas.py. In fact, had two others named pandas1.py and pandas2.py as well. I changed them all and then it worked perfectly:) Lesson learned.
So I am writing an answer myself. I just noticed that I created a file random.py in my project which was creating a conflict with random.py in pandas package. Renaming my current file to something else worked for me :)
I faced the same problem and the solution that worked for me is as below.
Initially I installed the pandas and numpy with a regular user account. It installed library but there were few conflicts. So I uninstalled the libraries using pip uninstall package then installed them back as sudo account using sudo -H pip install package.
I hope it helps other people facing similar issue.
You literally just need to make sure that you have no ".py" files that have names of the same names of the packages. Like pandas.py, numpy.py etc..
Try print(pd)
Make sure you are getting this kind of output
<module 'pandas' from 'C:\\Users\\adarsh\\AppData\\Local\\Programs\\Python\\Python38\\lib\\site-packages\\pandas\\__init__.py'>
Otherwise, there may be another python file named pandas in your current working directory
For more click here
I also got the same problem but when I made a separate directory for the python file problem was solved. Make sure there is no other python file in your directory.
It is either the file named pandas.py or csv.py in the same directory or a name similar to one of the pandas files(which are many) in the same directory. I had a file named tokenize.py which it did not like. You can try checking in the pandas directory (search results in pandas) whether there are similar file names.
your project contains pandas.py possibly. rename it and try again.. would work.
Put your csv file in the directory or folder where your python project files are . This solution worked for me.
Make sure that, the library imported and the filename you are executing or the file you are having in the directory wont be having the samename.
Explainer Screenshot
Try to rename the Filename as Pandas1 or Pandas_Tutorial.py. It will solve the Problem.

CSjark: NameError name 'Platform' is not defined

I'm usually not working with Python (but have ability to read the code).
I'm trying to use csjark.
All the dependencies were installed correctly.
When executing csjark.py I'm receiving following error:
NameError name 'Platform' is not defined
The import is done with from platform import Platform
All the *.py files are located in the same folder.
I don't have any issue similar statements for other imports.
Importing with import platform is working, but latter I can't use any parameter from the class.
Please suggest ways to resolve the issue with the platform.py file.
Maybe your program is importing the wrong platform.py, for example this one:
https://docs.python.org/2/library/platform.html
which doesn't seem to have a Platform class. Try renaming the platform.py to something else and importing from that to see if this is the issue.

Including xlrd/xlwt/xlutils with modules outside of python installation

I'm self-taught in the Python world, so some of the structural conventions are still a little hazy to me. However, I've been getting very close to what I want to accomplish, but just ran into a larger problem.
Basically, I have a directory structure like this, which will sit outside of the normal python installation (this is to be distributed to people who should not have to know what a python installation is, but will have the one that comes standard with ArcGIS):
top_directory/
ArcToolbox.tbx
scripts/
ArcGIStool.py (script for the tool in the .tbx)
pythonmod/
__init__.py
general.py
xlrd/ (copied from my own python installation)
xlwt/ (copied from my own python installation)
xlutils/ (copied from my own python installation)
So, I like this directory structure, because all of the ArcGIStool.py scripts call functions within the pythonmod package (like those within general.py), and all of the general.py functions can call xlrd and xlwt functions with simple "import xlrd" statements. This means that if the user desired, he/she could just move the pythonmod folder to the python site-packages folder, and everything would run fine, even if xlrd/xlwt/xlutils are already installed.
THE PROBLEM:
Everything is great, until I try to use xlutils in general.py. Specifically, I need to "from xlutils.copy import copy". However, this sets off a cascade of import errors. One is that xlutils/copy.py uses "from xlutils.filter import process,XLRDReader,XLWTWriter". I solved this by modifying xlutils/copy.py like this:
try:
from xlutils.filter import process,XLRDReader,XLWTWriter
except ImportError:
from filter import process,XLRDReader,XLWTWriter
I thought this would work fine for other situations, but there are modules in the xlutils package that need to import xlrd. I tried following this advice, but when I use
try:
import xlrd
except ImportError:
import os, sys, imp
path = os.path.dirname(os.path.dirname(sys.argv[0]))
xlrd = imp.load_source("pythonmod.xlrd",os.path.join(path,"xlrd","__init__.py"))
I get a new import error: In xlrd/init.py, the info module is called (from xlrd/info.py), BUT when I use the above code, I get an error saying that the name "info" is not defined.
This leads me to believe that I don't really know what is going on, because I thought that when the init.py file was imported it would run just like normal and look within its containing folder for info.py. This does not seem to be the case, unfortunately.
Thanks for your interest, and any help would be greatly appreciated.
p.s. I don't want to have to modify the path variables, as I have no idea who will be using this toolset, and permissions are likely to be an issue, etc.
I realized I was using imp.load_source incorrectly. The correct syntax for what I wanted to do should have been:
imp.load_source("xlrd",os.path.join(path,"xlrd","__init__.py"))
In the end though, I ended up rewriting my code to not need xlutils at all, because I continued to have import errors that were causing many more problems than were worth dealing with.

Categories