I have spent the last hour trying to fix the pandas import error, but with no success.
Python version: 3.8.3
Pandas version: 1.0.4
I use Visual Studio code. Importing other modules works (tried with numpy). Pandas is installed using pip.
The error:
Traceback (most recent call last):
File "c:/Users/38599/Desktop/course1/moduli.py", line 34, in <module>
import pandas
File "C:\Program Files (x86)\Python38-32\lib\site-packages\pandas\__init__.py", line 180, in <module>
import pandas.testing
File "C:\Program Files (x86)\Python38-32\lib\site-packages\pandas\testing.py", line 5, in <module>
from pandas._testing import (
File "C:\Program Files (x86)\Python38-32\lib\site-packages\pandas\_testing.py", line 404, in <module>
RANDS_CHARS = np.array(list(string.ascii_letters + string.digits), dtype=(np.str_, 1))
AttributeError: module 'string' has no attribute 'ascii_letters'
I've tried downgrading pandas and reinstalling it, but nothing fixed the error for me.
Does anyone have any ideas?
remove the line with string.ascii_letters from the code.. as the error says
I faced same issue. I did the following and it worked.
select the py file you want to execute, for vs code menu select Run=>Start Debugging, it works perfectly. I think it is a bug in the current vs code
Do you have a file named string.py somewhere in your workspace, by chance?
If yes, that would be the problem. By Python rules, such a file is also a module named string, so whenever anything is trying to import string, your file gets loaded instead of the standard library module with the same name.
Related
First time Python user, so apologies if I am misunderstanding something basic like how libraries are accessed (I am an R user).
Using a colleague's code (which works on his end) and trying to load the the following:
from reportlab.lib import colors
results in the following error:
Traceback (most recent call last):
File "Box\Py\Python3\Py3_StaticMain.py", line 32, in <module>
from reportlab.lib import colors
File "C:\Program Files\Python310\lib\site-packages\reportlab\lib\colors.py", line 44, in <module>
from reportlab.lib.utils import asNative, isStr, rl_safe_eval
File "C:\Program Files\Python310\lib\site-packages\reportlab\lib\utils.py", line 389, in <module>
haveImages = Image is not None
NameError: name 'Image' is not defined
Pillow and reportlab are installed. After searching online I found similar error reports and the solution was to add this line prior to the previous library call (again, my colleague does need this on his end):
from PIL import Image
However, this did not fix the problem, the error persists. Also of note both these lines get greyed out by PyCharm which apparently means that these libraries are already loaded so these lines are unnecessary? It is counter intuitive that an unnecessary command would cause an error. The other libraries imported are os.path, sys, datetime, and tkinter. Also, even if I just put these two lines in a new py file I get the same behavior: greyed out and error.
Not a very satisfying answer but after uninstalling and reinstalling both Python and the IDE everything worked.
My background is Java/C++ and I'm very new to python. I'm trying to import pyodbc to my project which works alright if I do it via command line.
import odbc
However, when I try to do the same in pydev/eclipse, i get the following error which I cannot find a solution to. I suspect this may have something to do with Eclipse setup
Traceback (most recent call last):
File "C:\Users\a\workspace\TestPyProject\src\helloworld.py", line 2, in <module>
import pyodbc
File "C:\Users\a\AppData\Local\Continuum\Anaconda3\Lib\site-packages\sqlalchemy\dialects\mssql\pyodbc.py", line 105, in <module>
from .base import MSExecutionContext, MSDialect, VARBINARY
ImportError: attempted relative import with no known parent package
I'm really stuck here and any hints will be appreciated!
An incorrect library was imported into the External Libraries list of the project. After fixing it, the import is working.
I have a weird issue. Whenever I import pandas, I get an AttributeError:
import pandas as pd
AttributeError: 'module' object has no attribute 'read_sql_query'
That error message is from an old python session when I was playing around with pandas. I'm now unable to load pandas as all, I just get that attribute error. Here's the full stacktrace:
>>> import pandas as pd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tsd/p19/home/p19-jonasmst/.local/lib/python2.7/site-packages/pandas/__init__.py", line 13, in <module>
__import__(dependency)
File "/cluster/software/VERSIONS/python_packages-2.7_3/cluster/software/VERSIONS/python2-2.7.9/lib/python2.7/site-packages/pytz/__init__.py", line 29, in <module>
from pkg_resources import resource_stream
File "build/bdist.linux-x86_64/egg/pkg_resources.py", line 74, in <module>
File "parser.py", line 12, in <module>
asid_occurrences = pd.read_sql_query(sql_find_asid_occurrences, db)
AttributeError: 'module' object has no attribute 'read_sql_query'
Possibly relevant info:
I'm on a cluster and have to load python2 as a module in order to import pandas.
I'm logged in to a Linux VM on the cluster through a Thinlinc client (via FireFox), from which I'm again SSHed into another Linux VM.
I've tried module purge and then module load python2 again, to no avail.
I've tried to log out from the VM and back in again, to no avail.
I was using VIM when I first got the AttributeError.
Anyone have any idea what's going on here? Thanks!
Update:
This seems to happen only when I'm in the directory where I got the error earlier. Moving to another directory allows me to import pandas just fine. There are no hidden files or anything in that directory, though, so I don't understand what's going on here.
The problem was that my script was called parser.py which probably conflicts with something in pandas. Similar issue.
My script was running well before. I had a series of crashes on some scripts where I had to fix some things. However, all of a sudden, I can't even run a script and my sublime text shell is displaying this error. I tried chasing down the files in my /Library/...python... files to see if any files got renamed or any part of the module scripts got renamed or something, but have yet to figure it out. What can I try. Or better question, what did I do all of a sudden to cause this, and how would I undo it? It was literally working fine less than an hour ago not to mention all day.
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/numerictypes.py", line 101, in <module>
import numbers
File "/Users/peter/Documents/Trading/Code/AlphaModelVer1/numbers.py", line 2, in <module>
import Quandl
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Quandl/__init__.py", line 11, in <module>
from .Quandl import (
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Quandl/Quandl.py", line 11, in <module>
import pandas as pd
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/__init__.py", line 7, in <module>
from . import hashtable, tslib, lib
File "pandas/src/numpy.pxd", line 157, in init pandas.hashtable (pandas/hashtable.c:22984)
AttributeError: 'module' object has no attribute 'dtype'
EDIT:
Thanks to the comments! I had tried re-installing pandas, numpy, and Quandl. But it was all due to the fact I had renamed a helper script numbers.py, when it needed to draw that from within. Hint to all future users: Do not rename python scripts to existing module types.
I've had a similar error that was caused by installing a package while I had IPython running. Try reinstalling Pandas using pip (make sure all Python instances are closed) and maybe you'll have to reiinstall hashtable as well, I can't remember.
try to reinstall this Quandl library
File
"/Users/peter/Documents/Trading/Code/AlphaModelVer1/numbers.py", line
2, in
import Quandl
pip uninstall Quandl
pip install Quandl
This problem occurred with me in spite of not naming the script name to the existing module name. I tried the following and it worked.
Instead of "import Quandl", I did "from Quandl import Quandl" and it worked for me!
Im trying to figured out this problem. Yesterday I installed PyScripter and since then, scripts doesnt work. When I run any script (in PyScripter or IDLE) and trying to "import arcpy", it gets this error:
import arcpy
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import arcpy
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\__init__.py", line 17, in <module>
from geoprocessing import gp
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\__init__.py", line 14, in <module>
from _base import *
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 14, in <module>
import arcgisscripting
File "C:\Python26\ArcGIS10.0\lib\arcgisscripting.py", line 81, in <module>
from RuntimeError import RuntimeError
ImportError: No module named RuntimeError
Have somebody any suggestion how to fix it?
Thanks to all very much!
Sounds like the module is not installed properly (or at all). To verify, do the following:
open a shell and start the python shell by typing python
(If this doesn't display an error, check that python is added to your system path or if it is even installed at all. But if you've used Python on said machine maybe system path issue)
enter the commmand help('modules')
review the list of modules that's returned to see whether arcpy is included
if not, then you may have to reinstall the module