This is the error that appears when I try to import other tkinter files with the use of a button:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Feargus\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:\Users\Feargus\Documents\COMPUTING\games\BIG_MAIN_MENU.py", line 13, in settingsButton
import SETTINGS.py
ModuleNotFoundError: No module named 'SETTINGS.py'; 'SETTINGS' is not a package
Here is the code which imports the files:
def startGame():
import GAME.py
def settingsButton():
import SETTINGS.py
quit()
def leaderBoard():
import Leaderboard.py
quit()
def endGame():
quit()
The problem is that you're adding the .py extensions for all your import statements. You just need the file names when importing Python modules so
import SETTINGS instead of import SETTINGS.py
would be the correct way to format it.
The same goes to the other imports as well.
Related
I try to call from main.py a function in router.py that will call a function in calculator.py that will try to modify a variable in main.py
Something that in C# is achieved for example by creating a public static variable, I don't know how to achieve it in Python.
This is my file main.py:
import router
# create a table
global mytable
mytable = []
router.CallAnotherFunction("testing data")
This is my file router.py:
import calculator
def CallAnotherFunction(sample_data):
calculator.ModifyMainTable(sample_data)
This is my calculator.py:
import main
def ModifyMainTable(sample_data):
main.mytable = sample_data
This is the error I'm getting just when trying to execute router.CallAnotherFunction("testing data"):
AttributeError
partially initialized module 'router' has no attribute 'FindCorrectRoute' (most likely due to a circular import)
File "C:\Users\dimitri\Documents\Upwork\Footyamigo\main.py", line 37, in <module>
router.FindCorrectRoute(odd, next_match, local_data, visitor_data)
File "C:\Users\dimitri\Documents\Upwork\Footyamigo\calculator.py", line 3, in <module>
import main
File "C:\Users\dimitri\Documents\Upwork\Footyamigo\router.py", line 1, in <module>
import calculator
File "C:\Users\dimitri\Documents\Upwork\Footyamigo\main.py", line 5, in <module>
import router
What I really need is that mytable is accesible from all files and if I change it's value from any file I have to see the changes reflected on the others, I don't want to have different instances of the object.
Just solved it import __main__ as main inside calculator.py
I'm just doing some testing with windows-curses (2.3.0) and I was using this code and it gave me an error. The code and error are below.
Code:
import curses
from curses import wrapper
def main(stdscr):
stdscr.clear()
stdscr.addstr("Hello World")
stdscr.refresh()
stdscr.getch()
wrapper(main)
Error:
Traceback (most recent call last):
File "C:\Users\norbe\OneDrive\Desktop\Projects\Testing\Curses\curses.py", line 1, in <module>
import curses
File "C:\Users\norbe\OneDrive\Desktop\Projects\Testing\Curses\curses.py", line 2, in <module>
from curses import wrapper
ImportError: cannot import name 'wrapper' from partially initialized module 'curses' (most likely due to a circular import)
The code already imported curses and its context (methods and variables) binded to it.
It means that wrapper is already identified by python.
Try this :
import curses
wrapper = curses.wrapper
I've recently had a problem with (i think) the os module in python:
Traceback (most recent call last):
File "main.py", line 9, in <module>
api = getApi(os.environ['consumer_key'], os.environ['consumer_secret'], os.environ['access_token_key'], os.environ['access_token_secret'])
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\os.py", line 681, in __getitem__
raise KeyError(key) from None
KeyError: 'consumer_key'
my code of main.py is:
from config import getApi
import os
import sys
import time
print()
api = getApi(os.environ['consumer_key'], os.environ['consumer_secret'], os.environ['access_token_key'], os.environ['access_token_secret'])
my code of config.py is
import twitter
import os
def getApi(consumer_key, consumer_secret, access_token_key, access_token_secret):
return twitter.Api(consumer_key='*********',
consumer_secret='*********',
access_token_key='*********',
access_token_secret='*********')
Send tweets with the postUpdate is possible if I write the keys in the main.py but when I put the keys in the config.py, it don't works
Can anyone help me please ?
It's not an error with the os module, the keys simply aren't in the enviroment. If you're using a .env file you should use a module like dotenv to load the file.
Whenever I try to run my python code this Import Error occurs, I am relatively new to python and was wondering what was the most efficient way to fix it and why is it happening in the first place?
Traceback (most recent call last):
File "C:/Users/Admin/Documents/1 Tor/logger.py", line 2, in <module>
import pythoncom
File "C:\Python34\lib\site-packages\pythoncom.py", line 2, in <module>
import pywintypes
File "C:\Python34\lib\site-packages\win32\lib\pywintypes.py", line 124, in <module>
__import_pywin32_system_module__("pywintypes", globals())
File "C:\Python34\lib\site-packages\win32\lib\pywintypes.py", line 98, in __import_pywin32_system_module__
raise ImportError("No system module '%s' (%s)" % (modname, filename))
ImportError: No system module 'pywintypes' (pywintypes34.dll)
This is my original code:
import pyHook
import pythoncom
import sys
import logging
file_log = 'C:\\Users\\Admin\\Documents\\1 Tor\\log.txt'
def OnKeyboardEvent(event):
logging.basicConfig(filename=file_log, level=logging.DEBUG, format='% (message)s')
chr(event.Ascii)
logging.log(10,chr(event.Ascii))
return True
hooks_manager = pyHook.HookManager()
hooks_manager.KeyDown = OnKeyboardEvent()
hooks_manager.HookKeyboard()
pythoncom.PumpMessages()
pythoncom requires a package named pywintypes, which isn't yet installed. You'll need to download and install that. Since you're missing one package, you may be missing others. Check the pythoncom documentation to see whether there is a list of dependencies that you have to install first.
So I have a main.py file inside /home/richard/projects/hello-python directory:
import sys
sys.path.append('/home/richard/projects/hello-python')
from Encode import Ffmpeg
x = Ffmpeg()
x.encode()
I have then created a package in the /home/richard/projects/hello-python/Encode directory:
__init__.py
Ffmpeg.py
Init file is empty. Ffmpeg.py file contains:
class Ffmpeg(object):
i = 150
def __init__(self):
print "i am constructor"
def encode(self):
print "hello world"
Now I run the main.py script like this:
python main.py
I get this output:
richard#richard-desktop:~/projects/hello-python$ python main.py
Traceback (most recent call last):
File "main.py", line 5, in <module>
x = Ffmpeg()
TypeError: 'module' object is not callable
richard#richard-desktop:~/projects/hello-python$
I think there is some problem with my sys.path so my module cannot be imported correctly but I am not sure how to fix it.
from Encode.Ffmpeg import Ffmpeg