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

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__)

Related

How to resolve ModuleNotFoundError: No module named 'pandas'

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.

pycharm pandas not module http client error in only some files, runs in console

Pycharm is giving me a
AttributeError: module 'http' has no attribute 'client'
when trying to load in pandas for a specific file. Strangely I only get this error if I also have the following import:
from sklearn import svm
pandas is with the Anaconda package suite, and runs/loads without the sklearn module, and in the console.
Python 3.7, Github extension, anaconda suite. I have tried reinstall pandas and sklearn to no avail. I have tried reordering my file structure to avoid python path issues.
python
import pandas
from sklearn import svm
I expect the code to compile as there are no noticeable syntax errors and even with the rest of the code commented out this still happens.
edit: it compiles when run in the console, so there appears to be an issue with the python path. Is there a way to investigate this more closely or directly control the python path? Also it compiles when I run with
import sklearn
but does not with
from sklearn import svm
if that helps narrow down the issue at all...
The problem appears to be that my file is named ssl.py I don't know if there's another file called that which was cauing path issues but it seems to be fixed now...

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

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.

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.

Problems importing a python module in Maya and it being recognized

Still learning certain things about Python... I am having issues recognizing my Python script in my scripts dir. First, I checked to see that my path is set correctly:
import sys
for pythonPath in sys.path:
print pythonPath
And C:/Users/..../Documents/maya/2014-x64/scripts is listed, which is where I am placing swap.py
In Maya's script editor I am typing the following:
import swap
reload(swap)
swap.printSomething()
I get:
Error: AttributeError: file line 3: 'module' object has no attribute 'printSomething' #
If I take the same code and throw it into a package...
C:/Users/..../Documents/maya/2014-x64/scripts/swapPackage/swap.py
And then call this, it works...
import swapPackage.swap as swap
reload(swap)
swap.printSomething()
Why? I am totally confused. Mel scripts even run fine from this location as well. I just can't get a simple python script to import and run.
Also something I noticed. Even though I can get this script to run in a package, the package name must be totally different than the module name. I can't have a package named this:
C:/Users/..../Documents/maya/2014-x64/scripts/swap/swap.py
but I can have one where the package name is different:
C:/Users/..../Documents/maya/2014-x64/scripts/swapPackage/swap.py
Ok folks, I was able to solve this by executing a print of my file, only to find out that it was sourcing a totally different version someone copied elsewhere. ARGH. This solves both issues, and makes sense why changing the package name from the module worked.
import swap
reload(swap)
print swap.__file__

Categories