Sorry if this is a dumb question but I'm trying to import and open a CSV using pandas in Python. Whenever I hit run I get the syntax error "cannot import name 'unicode_literals'". I have no idea why that is happening and I haven't been able to find any source online which details what this error means.
This is my code:
import pandas as pd
with open(r"FILEPATH\File.csv") as rawData:
pd.read_csv(rawData)
Here is the Error:
C:\Anaconda3\python.exe "FILEPATH"
Traceback (most recent call last):
File "FILEPATH/Main.py", line 1, in <module>
import pandas as pd
File "C:\Anaconda3\lib\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:22997)
File "C:\Anaconda3\lib\site-packages\numpy\__init__.py", line 107, in <module>
from __future__ import division, absolute_import, print_function
File "C:\Anaconda3\lib\__future__.py", line 23, in <module>
from __future__ import unicode_literals
ImportError: cannot import name 'unicode_literals'
cannot import name 'unicode_literals'
Any suggestions for why this isn't working would be greatly appreciated.
You're on the right track! The only thing you have to do is add another parameter to open(). This would yield:
import pandas as pd
with open(r"FILEPATH\File.csv", encoding='utf-8') as rawData:
pd.read_csv(rawData)
Related
I have a portable python version to use within C:\temp
I wanted to test what version of pandas is included by writing a "scrip"t that import pandas as pd and pritns the pandas version.
I usually start python via the command line which now returned an "Syntax" error in a file that I have never touched.
c:\temp\XXXXApps\RAV\Portable Python 3.2.5.1\App>python.exe packagesTest.py
Traceback (most recent call last):
File "packagesTest.py", line 1, in <module>
import pandas as pd
File "c:\temp\XXXXApps\RAV\Portable Python 3.2.5.1\App\lib\site-packages\pandas\__init__.py", line 27, in <module>
from pandas.core.api import *
File "c:\temp\XXXXApps\RAV\Portable Python 3.2.5.1\App\lib\site-packages\pandas\core\api.py", line 14, in <module>
from pandas.core.frame import DataFrame
File "c:\temp\XXXXApps\RAV\Portable Python 3.2.5.1\App\lib\site-packages\pandas\core\frame.py", line 33, in <module>
from pandas.core.internals import (BlockManager,
File "c:\temp\XXXXApps\RAV\Portable Python 3.2.5.1\App\lib\site-packages\pandas\core\internals\__init__.py", line 1, in <module>
from pandas.core.internals.blocks import ( # io.pytables, io.packers
File "c:\temp\XXXXApps\RAV\Portable Python 3.2.5.1\App\lib\site-packages\pandas\core\internals\blocks.py", line 97
values: Union[np.ndarray, ExtensionArray]
^
SyntaxError: invalid syntax
I am completly new when it comes to these kind of "Error messages". maybe someone can help with what is wrong here.
Pandas Core Internal block
I'm trying to run a python program that uses the pandas library on Mac OS, and I get the next error
adan_vazquez#EPAM-C02G513RML7L automate_python % python3 test_pandas.py
Traceback (most recent call last):
File "/Users/adan_vazquez/Desktop/automate_python/test_pandas.py", line 1, in <module>
import pandas as pd
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/__init__.py", line 11, in <module>
__import__(dependency)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/__init__.py", line 155, in <module>
from . import random
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/random/__init__.py", line 180, in <module>
from . import _pickle
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/random/_pickle.py", line 1, in <module>
from .mtrand import RandomState
File "mtrand.pyx", line 1, in init numpy.random.mtrand
File "bit_generator.pyx", line 40, in init numpy.random.bit_generator
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/secrets.py", line 19, in <module>
from random import SystemRandom
File "/Users/adan_vazquez/Desktop/automate_python/random.py", line 3, in <module>
print(random.randint(1,10))
AttributeError: partially initialized module 'random' has no attribute 'randint' (most likely due to a circular import)
Here's the code that I'm trying to run:
import pandas as pd
simpsons = pd.read_html('https://en.wikipedia.org/wiki/List_of_The_Simpsons_episodes_(seasons_1%E2%80%9320)')
print(len(simpsons))
print("Hola")
As you can see is not a big code and I'm getting the error, I already updated my pip3 and my python3 versions, also I reinstall pandas and I keep getting the error, if I try to import pandas in the python terminal I get the same error and I don't know what's causing it, I hope you can help me, thanks in advance.
The error message says it is a circular import error.
Try renaming /Users/adan_vazquez/Desktop/automate_python/random.py with something other than random
According to the error, it is a circular import which is causing the problem.
Since I do not have the details of your project structure or file name, my best guess is that you accidentally named your working file the same as the module name and those modules depend on each other.
I'm just learning Python.
I have a file with the following content
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
If I name this file csv2.py and call:
python csv2.py
... it works. But if I name this file csv.py and run:
python csv.py
It triggers this exception:
C:\Git\algotrading [master ≡ +3 ~0 -0 !]> python csv.py
Traceback (most recent call last):
File "csv.py", line 2, in <module>
import matplotlib.pyplot as plt
File "C:\Users\andrerpena\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\pyplot.py", line 29, in <module>
import matplotlib.colorbar
File "C:\Users\andrerpena\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\colorbar.py", line 34, in <module>
import matplotlib.collections as collections
File "C:\Users\andrerpena\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\collections.py", line 36, in <module>
import matplotlib.mlab as mlab
File "C:\Users\andrerpena\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\mlab.py", line 172, in <module>
import csv
File "C:\Git\algotrading\csv.py", line 2, in <module>
import matplotlib.pyplot as plt
AttributeError: module 'matplotlib' has no attribute 'pyplot'
It took me like 40 minutes to figure out the problem. I mean.. Figure out the problem had to do with the name of the file.
Why is that happening?
It looks like matplotlib.pyplot through various imports needs mlab.py which calls "import csv". This should find a file (that is not yours) called csv but since you have renamed your file to csv.py it is attempting to import that instead, overriding the required import and messing up the import for matplotlib.pyplot.
csv.py is built into python and thus is restricted.
If you run a python interpreter and try to import csv, you'll succeed without having to download anything new.
Like igoldthwaite and Daniel Dobalian said:
If you create a file named: csv.py with the content:
import csv
print(csv)
And run:
python csv.py
You will see that this file import itself:
<module 'csv' from '/home/your/folder/csv.py'>
Also, if you do the same thing with other modules, you will find the same result:
import subprocess
print(subprocess)
Result:
<module 'subprocess' from '/home/your/folder/subprocess.py'>
And finally, if you do it with a file named matplotlib.py, you will get the same result:
import matplotlib
print(matplotlib)
# <module 'matplotlib' from '/home/your/folder/matplotlib.py'>
I'm trying to run a script in python, and I'm getting an "invalid elf header" error. I have a series of scripts that call one another, I'll go ahead and include those as well:
import sys
sys.path.append("/home/smh/Linux/Desktop/gras-03-03/python")
from python_utilities import GRASTestor
which calls GRASTestor.py
from BaseTestor import BaseTestor
import numpy as np
import hepunit as unit
from gdml_writer import gdml_writer
from GDMLGeometryBuilder import GDMLGeometryBuilder
from GRASMacroBuilder import GRASMacroBuilder,GRASRMCMacroBuilder
from Plotters import Plotter
import os
import sys
import SpenvisCSVFileHandler
which calls SpenvisCSVFileHandler.py
import string
import Spenvis.so
import os
from numpy import *
which is where we get our error, specifically with the line "import Spenvis.so"
/home/smh/Linux/Desktop/gras-03-03/python/python_utilities
Traceback (most recent call last):
File "perform_gras_rmc_tests.py", line 6, in <module>
from python_utilities import GRASTestor
File "/home/smh/Linux/Desktop/gras-03-03/python/python_utilities/GRASTestor.py", line 19, in <module>
import SpenvisCSVFileHandler
File "/home/smh/Linux/Desktop/gras-03-03/python/python_utilities/SpenvisCSVFileHandler.py", line 8, in <module>
import Spenvis.so
ImportError: /home/smh/Linux/Desktop/gras-03-03/python/python_utilities/Spenvis.so: invalid ELF header
And I'm not certain why it's not working. Any suggestions would be appreciated!
Never mind. Upon looking at the file architecture, it appears the file Spenvis.so is mac specific for some reason. Just need to get the correct file, then.
Please forgive my simple question. I have just started to use Matplotlib and I am having some difficulty.
I can run the following with the interpretor without problems:
>>> from pylab import *
>>> plot([1,2,3])
>>> show()
The above code generates a beautiful graph.
However, if I place the following code inside a file and run it, I get an error:
#!/usr/bin/env python
# encoding: utf-8
import sys
import os
from pylab import *
plot([1,2,3])
show()
Here is the error message:
Traceback (most recent call last):
File "/Users/sbrown/Desktop/new1.py", line 12, in <module>
from pylab import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pylab.py", line 1, in <module>
from matplotlib.pylab import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.py", line 133, in <module>
from matplotlib.rcsetup import (defaultParams,
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/rcsetup.py", line 19, in <module>
from matplotlib.colors import is_color_like
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/colors.py", line 54, in <module>
import matplotlib.cbook as cbook
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/cbook.py", line 15, in <module>
import new
File "/Users/sbrown/Desktop/new.py", line 2, in <module>
plot([1,2,3])
NameError: name 'plot' is not defined
>>>
Any idea what the problem could be? Thanks in advance for any help you can provide!
Looks like you have a file on your Desktop that is shadowing the standard Python new module:
>>> import new
>>> new
<module 'new' from '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/new.pyc'>
Rename or remove $HOME/Desktop/new.py and try again.