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
Related
I am not able to run a audio file automatically when the program started i saw playsound module but it is giving me error
Traceback (most recent call last):
File "main.py", line 7, in <module>
playsound('Music/videoplayback(2).mp3')
TypeError: 'module' object is not callable
my code
import random
import playsound
#Creating a KBC Special Quiz!
print("'\033[1m'"+"Welcome To Kaun Banega Crorepati!")
print()
print()
playsound('Music/videoplayback(2).mp3')
i am using repl, even i am finding difficulties in pathing
You are using the playsound module instead of the playsound.playsound function.
The docs show you how to import it so it works like you expected, see below.
>>> from playsound import playsound
>>> playsound('/path/to/a/sound/file/you/want/to/play.mp3')
I am using VS code for my python and I am getting an error saying
'Traceback (most recent call last):
File "c:\Users\thecodeadd\OneDrive\Documents\Program\Python\Pyton programs\Fidget spinner.py", line 1, in <module>
import tkinter
File "c:\Users\thecodeadd\OneDrive\Documents\Program\Python\Pyton programs\tkinter.py", line 2, in <module>
win= Tk()
NameError: name 'Tk' is not defined'
I have repaired my python file and searched on the internet but I have not been able to find any solutions for this.
You didn't import the Tk class. Intead, you imported the tkinter module. To use the Tk class from the module you need to use win = tkinter.Tk().
It also looks like you named your file tkinter.py, which means that even if you do the above it won't work because your tkinter.py will get imported instead of the tkinter module. You should rename your file to something else besides tkinter.py.
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.
I have the problem on importing the class in the same package, and it seems not a circular dependency problem. So I'm really confused now.
my-project/
lexer.py
exceptions.py
I declared an exception in exceptions.py and wants to use it in lexer.py:
exceptions.py:
class LexError(Exception):
def __init__(self, message, line):
self.message = message
self.line = line
and in lexer.py:
import re
import sys
from exceptions import LexError
...
It shouldn't be circular dependency since lexer.py is the only file has import in it.
Thanks!!
exceptions conflicts with builtin module exception.
>>> import exceptions
>>> exceptions.LexError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'LexError'
>>> from exceptions import LexError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name LexError
Use different module name.
I'm trying this simple code:
import requests
print requests.__file__
r = requests.get('https://github.com/timeline.json')
It works flawlessly on the command line when I type the lines one by one, but not whenen when I execute it as a script or in Sublime Text 2. Here's the stack trace:
C:\Python27\lib\site-packages\requests\__init__.pyc
Traceback (most recent call last):
File "C:\Users\Bruce\Desktop\http.py", line 1, in <module>
import requests
File "C:\Python27\lib\site-packages\requests\__init__.py", line 53, in <module>
from requests.packages.urllib3.contrib import pyopenssl
File "C:\Python27\lib\site-packages\requests\packages\__init__.py", line 3, in <module>
from . import urllib3
File "C:\Python27\lib\site-packages\requests\packages\urllib3\__init__.py", line 16, in <module>
from .connectionpool import (
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 15, in <module>
from http.client import HTTPConnection, HTTPException
File "C:\Users\Bruce\Desktop\http.py", line 3, in <module>
r = requests.get('https://github.com/timeline.json')
AttributeError: 'module' object has no attribute 'get'
[Finished in 0.2s with exit code 1]
Answers on 'Module object has no attribute 'get' Python error Requests? didn't help much.
Could this be some error in my ST2 Python build system? I tried removing all requests modules in case there were multiples of them by using pip and reinstalled them.
Edit After reading the stacktrace again, you can see that urllib3 tries to import something from the http module. Your file is called http.py and is thus imported instead of the expected one.
The actual error happens because of the circular nature of the import. Since requests hasn't finished importing completely yet. The get function in requests isn't defined yet when the http import reaches import requests again.
Note: You will also want to always guard your entry point with the if __name__ == '__main__' construct. This will often avoid nasty errors for unsuspecting future developers (including yourself).