I have a python project that do the following import
from PyQt4 import QtGui, QtCore
I would like to change the cursor's shape at certain point, for example, I would create a new QCursor object
self.cursor_oh = QCursor()
self.cursor_oh.setShape()
most documentation and searches I did indicate to just set the shape using enum type such as:
Qt.OpenHandCursor
But, such enum is not recognized and it always results in the following error message:
AttributeError: 'module' object has no attribute 'OpenHandCursor'
So, what am I missing here?
Apparently, the Qt namespace is under QtCore, so by adding the QtCore qualifier, it found all of the Qt cursor types.
I haven't tried this in PyQt4 but in PyQt6 it has to be:
QtCore.Qt.CursorShape.OpenHandCursor
Related
I am running Python 3.7.6 shell and have the library numpy installed correctly.
In my shell I type:
import numpy as np
and can use numpy however I desire. I then proceed to import 'my_lib.py' which contains:
def softmax(x):
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum(axis=0)
In my shell I can call the function softmax(x) but I immediately get the error
NameError: name 'np' is not defined
My hypothesis here would be I've imported numpy into 'shell scope' and i've also imported softmax(x) into 'shell scope' so everything should be happy. To fix this problem I have to add
import numpy as np
into 'my_lib.py'.
How come I have to import numpy twice?
The code in each module can only use identifiers (names) that have be defined in or imported into that module. The global dict in each module only contains names global to that module. It might better be called the module dict or modular dict, but the name goes back to when there were no modules in computing.
You might benefit from reading https://docs.python.org/3/tutorial/modules.html and probably elsewhere in the tutorial.
(None of this has anything to do with the editor you use to write code or the IDE or shell you use to pass code to Python.)
I am new to python. I am using the anaconda prompt to run my code. I am trying to import a class from another module but I keep getting errors such as cannot find reference to the class.
P.S please don't negative mark this, or else I will lose the privilege of asking questions
I have already provided an __init__ function.
The module itself runs just fine.
I have used from parser import Parser
I have used from parser import * statement as well
My Parser class
class Parser(object):
def __init__(self, tokens):
self.tokens = tokens
self.token_index = 0
My main class
from parser import Parser
I expected it to normally import the class, but it is unable to.
I keep getting the cannot import name 'Parser' from 'parser' (unknown location) when I use from parser import Parser
parser is also the name of a Python module in the standard library.
It's likely there is a name conflict, and that Python is importing the module from the standard library.
I changed the module name to mparser and it works!
I'd imagine this is a simple question, but why is (Python 3.7.1)
import matplotlib.cm
not the same as
from matplotlib import cm
?
Having tried the first method I get this error when calling the module:
NameError: name 'cm' is not defined
The second method works just fine. Is this a subtlety in the import statement or in the nature of a module?
In the first case you import only part of the matplotlib module but maintain the namespace matplotlib.cm - so you have to use the lengthy namespace to access cm.
In the second case you import only part of the matplotlib module into cm namespace.
Effectively, to get to ScalarMappable (an example) class in matplotlib.cm you have to use:
matplotlib.cm.ScalarMappable
cm.ScalarMappable
I want to do the semantic checking for a language and i use ANTLR4 to generate parser and visitor class. However i met a problem.
If i use this method print(type(newList[0].expression()))
I will get a type like this <class 'IDILParser.IDILParser.IdenetExpressionContext'>
However, if i run the code below, i will get a error like this NameError: name 'IDILParser' is not defined
Can i ask how to fix this problem? Thanks!
from antlr4 import *
if __name__ is not None and "." in __name__:
from .IDILParser import IDILParser
else:
from IDILParser import IDILParser
class IDILVisitor(ParseTreeVisitor):
def visitAssign(self, ctx:IDILParser.AssignContext):
if type(newList[0].expression()) is IDILParser.IDILParser.IdenetExpressionContext:
...
You did from IDILParser import IDILParser, which means the IDILParser in your code already acutally refers to IDILParser.IDILParser.
So try taking away that one layer:
if type(newList[0].expression()) is IDILParser.IdenetExpressionContext:
...
Btw, when in doubt if your code is being run as a module or as a script (aka relative imports do work or not), you could also do the following:
try:
from .IDILParser import IDILParser
except ImportError:
from IDILParser import IDILParser
I'm trying to import QHash class from PyQt5.QtCore but no luck.
I got an error
AttributeError: module 'PyQt5.QtCore' has no attribute 'QHash'
Could anyone know how to do it?
Thanks a lot.