Getting "AttributeError: partially initialized module 'csv'" trying to use csv module - python

import csv
data = csv.reader(open('spy.csv'),delimiter=';')
lista = []
for fila in data:
lista.append(fila)
this is the error:
PS C:\Users\User\Desktop\primer proyecto> & C:/Users/User/AppData/Local/Microsoft/WindowsApps/python3.9.exe "c:/Users/User/Desktop/primer proyecto/FINANZAS/csv.py"
Traceback (most recent call last):
File "c:\Users\User\Desktop\primer proyecto\FINANZAS\csv.py", line 1, in <module>
import csv
File "c:\Users\User\Desktop\primer proyecto\FINANZAS\csv.py", line 6, in <module>
data = csv.reader(open('spy.csv'),delimiter=';')
AttributeError: partially initialized module 'csv' has no attribute 'reader' (most likely due to a circular import)
PS C:\Users\User\Desktop\primer proyecto>

Related

Shelve Module considered as shelve.py

enter image description here
Hey, i am new in python
i am trying to import shelve module but pycharm say
Traceback (most recent call last):
File "C:\Users\mavic\PycharmProjects\ABS-CH8\shelve.py", line 1, in
import shelve
File "C:\Users\mavic\PycharmProjects\ABS-CH8\shelve.py", line 2, in
namavariale = shelve.open('mydata')
AttributeError: partially initialized module 'shelve' has no attribute 'open' (most likely due to a circular import)
i am trying to import shelve module not shelve.py, what is the solution?

Unable to import array library in Python

So I've started to learn Python recently and right now I've been trying to learn arrays, but unable to use the array function after importing the array library.
I have tried four different methods to use the array function but failed successfully.
Method 1:
import array
nums = array.array('i', [])
#rest of the code
Output 1:
Traceback (most recent call last):
File "array.py", line 2, in <module>
import array
File "/home/prince/Desktop/python-basics/array.py", line
4, in <module>
nums = array.array('i', [])
TypeError: 'module' object is not callable
Method 2:
import array as a
nums = a.array('i', [])
#rest of the code
Output 2:
Traceback (most recent call last):
File "array.py", line 2, in <module>
import array as a
File "/home/prince/Desktop/python-basics/array.py", line
4, in <module>
nums = a.array('i', [])
AttributeError: partially initialized module 'array' has
no attribute 'array' (most likely due to a circular
import)
Method 3:
from array import array
nums = array('i', [])
#rest of the code
Output 3:
Traceback (most recent call last):
File "array.py", line 2, in <module>
from array import array
File "/home/prince/Desktop/python-basics/array.py", line
2, in <module>
from array import array
ImportError: cannot import name 'array' from partially
initialized module 'array' (most likely due to a circular
import) (/home/prince/Desktop/python-basics/array.py)
Method 4:
from array import *
nums = array('i', [])
Output 4:
Traceback (most recent call last):
File "array.py", line 2, in <module>
from array import *
File "/home/prince/Desktop/python-basics/array.py", line
4, in <module>
nums = array('i', [])
NameError: name 'array' is not defined
And after compilation, every time another folder is automatically created in my directory whose name is : pycache
And inside that folder there is a file named: array.cpython-38.pyc which I am unable to open. My editor says that it is because it either uses binary or unsupported text.
A few additional details if that helps:
Text Editor I Used: VS Code
My OS: Ubuntu 20.04LTS
Python Version: 3.8.5
All of the above imports fail due to the file name being same as the module name which you import. Pretty sure you can't have the same name as the module you're trying to import. Try renaming the filename array.py to something else and it should work.
About pycache folder, it contains the compiled bytecode for the python program. This shouldn't have anything to do with this problem.

Python/Json AttributeError: partially initialized module 'json' has no attribute

I am running Windows 10 with MS Code Python 3.7
I get the following message from my simple code block
Traceback (most recent call last):
File "c:/Users/marke/OneDrive/Desktop/Python Tutorial/json.py", line 1, in
module
import json File "c:\Users\marke\OneDrive\Desktop\Python Tutorial\json.py", line 6, in
jsText = json.loads(FileText) AttributeError: partially initialized module 'json' has no attribute 'loads' (most likely due to
a circular import)
My code
import json
jsFile = open("myjson.json","r")
FileText = jsFile.read
jsText = json.loads(FileText)
When you name your script the name of the module you try to import, python tries to imports your script first, which results in the Error

Getting TypeError while importing urllib

When I write the following line of code
import urllib
I get this error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/urllib.py", line 26, in <module>
import socket
File "socket.py", line 2, in <module>
s = socket.socket()
TypeError: 'module' object is not callable
After going through various questions on SO I tried these:
from urllib import urlopen
(Same error as above)
>>> urllib
Error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'urllib' is not defined
>>> urllib.urlopen()
Error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'urllib' is not defined
Please help. I get a similar error when I try to import urllib2, urllib3, requests.
You named your file socket.py, hiding the standard library socket module. Name it something else.

AttributeError: 'module' object has no attribute 'TemporaryFile'

I am new to python.And i am learning the standard library.
Whenever i run the code below , it always raise the AttributeError...
And it seems like there is something wrong with the import command.
Also , i try to run it on the interactive interpreator,and it works just fine.
The sample code
import tempfile
import os
#temp = tempfile.TemporaryFile()
temp = tempfile.mktemp()
print "tempfile","=>",temp
file = open(temp,"w+b")
file.write("*" * 1000)
file.seek(0)
print len(file.read()),"byte"
file.close()
try:
os.remove(temp)
except OSError:
pass
The error output
Traceback (most recent call last):
File "tempfile.py", line 1, in <module>
import tempfile
File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
tempfile = tempfile.mktemp()
AttributeError: 'module' object has no attribute 'mktemp'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 66, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 1, in <module>
from apport.report import Report
File "/usr/lib/python2.7/dist-packages/apport/report.py", line 12, in <module>
import subprocess, tempfile, os.path, urllib, re, pwd, grp, os
File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
tempfile = tempfile.mktemp()
AttributeError: 'module' object has no attribute 'mktemp'
Original exception was:
Traceback (most recent call last):
File "tempfile.py", line 1, in <module>
import tempfile
File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
tempfile = tempfile.mktemp()
AttributeError: 'module' object has no attribute 'mktemp'
My enviroment
ubuntu12.04
python2.7
Did you name your own file tempfile.py? If so, rename it, delete all your *.pyc files, and try again.
PS: providing the actual text of the error with the traceback would tell us these things.
Trying to access an attribute that does not belong to a class or function in a module raises an AttributeError exception, the attribute might have been deprecated in a later version of the Python interpreter being used. I suggest you check the version of the Python you're running and make sure your dir(module) includes the attribute you're trying to use

Categories