Shelve Module considered as shelve.py - python

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?

Related

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

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>

Unable to import one python file into another [duplicate]

is there a way to import multiple python files into a main python file?
I have a bunch of py files and each one has to run in the main python file and the data are saved into a json file.
This is what I tried and it gave me an error.
import light.py as light
Error:
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pi/Desktop/majorproject/pillar1.py", line 8, in <module>
import sensorkey.py as sensorkey
ImportError: No module named 'sensorkey.py'; 'sensorkey' is not a package
I have also tried specifying the path to the py file and it didn't work either and keeps giving an invalid syntax error.
import /home/pi/Desktop/json/light.py as light
Update:
I managed to fix the import error but i now, after importing this light.py file, i have to print out certain keys from a dictionary (key) into this new file then export it to a json file. I'm currently using TinyDB to do so. Here are my codes:
from tinydb import TinyDB, Query
import json
from light import key
with open("/home/pi/Desktop/json/sensortestest.json", 'w+'):
db = TinyDB('/home/pi/Desktop/json/sensortestest.json')
table = db.table('Light')
db.insert_multiple([{'Key 1' :key[lightkey]}, {'Key 2' : key[lightkeyID]}])
Error:
Traceback (most recent call last):
File "/home/pi/Desktop/majorproject/testertestest.py", line 12, in <module>
db.insert_multiple([{'Key 1' :key[lightkey]}, {'Key 2' : key[lightkeyID]}])
NameError: name 'lightkey' is not defined
The problem is I had already defined 'lightkey' in its own file already.
To include the dictionary, you could do this if your file location is in different directory (with caution of path.append as #Coldspeed mentioned):
import sys
sys.path.append("path/foo/bar/")
from light import *
If it is in same directory as current directory, you could just do:
from light import *
The syntax for importing your_filename.py, assuming it is in the same directory, is
import your_filename
In your case, it would be
import light
Note the absence of .py.
If your file is in a different directory, you'll need to do:
import sys
sys.path.append('path/to/dir/containing/your_filename.py')
import your_filename
Note that appending to sys.path is dangerous, and should not be done unless you know what you're doing.
Read more at the official docs for import.

Why does python shelf module give an error at start up when opening file

I have been using the python shelf module to store face encodings from the python face-recognition module below. I did this to make the live image recognition process faster.
I then imported these encodings in another script using the shelf module again, assigned them to a variable, and use them further down my script. This all works fine in the python idle environment and when I run it from the terminal. However, on startup, the shelf module fails to import the data. Can anyone tell me why this happens at start-up? The error I get on the log file is below. I have been stuck on it for a few days now. Is there a better way of storing and reusing the encodings? Thanks in advance.
the bit of code that fails on start-up but runs fine otherwise
import shelve
shelfFile = shelve.open('face_encodings')
known_encodings = shelfFile['known_encodings']
known_names = shelfFile['known_names']
shelfFile.close()
the error on startup
Traceback (most recent call last): File "/usr/lib/python3.7/shelve.py", line 111, in __getitem__
value = self.cache[key]
KeyError: 'known_encodings'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pi/Desktop/run/testing.py", line 4, in <module>
known_encodings = shelfFile['known_encodings']
File "/usr/lib/python3.7/shelve.py", line 113, in __getitem__
f = BytesIO(self.dict[key.encode(self.keyencoding)])
KeyError: b'known_encodings'
face-recognition module
https://pypi.org/project/face-recognition/

I am not able to use re module, my interpreter showing me error

My code is:
import re
s="An apple in a day."
print(re.search("in",s))
Error:
C:\Users\DELL\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/DELL/AppData/Local/Programs/Python/Python38-32/Lib/encodings/re.py
Traceback (most recent call last):
File "C:/Users/DELL/AppData/Local/Programs/Python/Python38-32/Lib/encodings/re.py", line 1, in <module>
import re
File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\Lib\encodings\re.py", line 3, in <module>
re.search("is",s)
AttributeError: partially initialized module 're' has no attribute 'search' (most likely due to a circular import)
Process finished with exit code 1
You name your file re.py so python confused which one to use when you call import re: your file or regexp library.
Try to rename your file to something else and not to choose other library names.

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

Categories