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.
Related
I have this script that I originally created in Jupyter Notebooks and I saved it later on as a .py file, then I used auto-py-to-exe to convert it to an exe file. When it runs it gives me this error:
ModuleNotFoundError: No module named 'pandas' [23712] Failed to execute script due to unhandled exception!
It's importing pandas in at the top of the script, and I had used pip install to install pandas, and I thought I had all of my python --path variables set correctly, but clearly something still isn't set right. Is this an issue of looking at the wrong directory?
This is apparently a somewhat common issue with auto-py-to-exe. I'd suggest reading through this blog post by the author for solutions to this and many other issues. If you skip to here, the suggested solution is as follows:
ModuleNotFoundError: No module named x / ImportError: No module named x
This means a particular module ('x' in this case) was not added to the package. I have seen this occur with packages in the pandas library and win32api; as long as you can identify the package (e.g. 'x'), then it is very easy to fix.
To fix this in the UI, open the advanced tab and find the --hidden-import input. Simply paste the module name into this input and then repackage. If the original error is still appearing, you have done this incorrectly.
I want to upload a csv file in pandas.
Here is the code :
import pandas as pd
df = pd.read_csv('iris.csv')
print(df)
I tried it with different files, too.
The csv file and the py file are located in the same location,
but every time this error pups up:
AttributeError: partially initialized module 'pandas' has no attribute 'read_csv' (most likely due to a circular import)
I tried the file location in read_cvs() too, but nothing changed.
and i tried uninstalling and reinstalling pandas
and also tried with putting csv file in all files like venv
but nothing changed
i tried the code and the csv file in visual studio code and it worked,
but i want it in pycharm
In your error, it shows that the error is coming from circular imports. Uninstall pandas using pip uninstall pandas and then install it with pip install pandas. Furthermore, read this article on circular imports and how to deal with them.
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.
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__)
I'm following a tutorial to call python code from a C++ program from the python docs.
Everything works just fine when trying to call the multiply example. Now if I add a line to the python source code importing a library, lets say openpyxl,
from openpyxl import load_workbook
I receive an error from python
ImportError: No module named openpyxl
I thought if I import a system library, I wouldn't have any problems, but I also get an error if I try to import datetime.
I don't have any error if I import the file from the python console. The openpyxl library is installed in my system.
So my question is: how to import python source code that needs to import packages?
EDIT: Ok, I forgot to mention something, I have not been completely honest with you guys, I'm sorry.
Trying to run the example I run into a problem: I couldn't make python found my multiply.py file, and the line PyImport_Import always return null.
My solution was to add the path in which I knew my python source was by using PySys_SetPath. The problem is that I just realized that this function doesn't append a new directory, it just overwrites the PYTHONPATH. So now python can find multiply.py, but absolutly anything else.
Of course I've deleted that line but now I have another question, why does python can't find my source if the file is just in the same directory of the C++ compiled program?
The I realized that my sys.path from my python console was a little different from the path showed in my embedded python: the first one had at the beginning of the list an empty string ''. I'm not a python expert, but when I add that line to my path I could import the multiply.py so it seems that was the reason I couldn't import modules that were located to relative to my executable was the missing of this empty path -but still don't know what it means-.
I have to thank to #paul-evans who give me the idea of adding the path to find my files.
This is what PYTHONPATH is for. You can set it as an environment variable containing a list module directories, or in the code itself something like:
import sys
sys.path.append("path/to/openpyxl/module")