PyQt 4 import in read-the-docs - python-2.7

I am currently in the process of getting the documentation of my code online via read-the-docs, however, getting read-the-docs to process my PyQt4 dependent modules seems problematic.
My project has the following structure:
pkg
pkg/__init__.py
pkg/modules/
pkg/modules/__init__.py
pkg/modules/somemodules.py
pkg/gui/__init__.py
pkg/gui/someGUImodules.py
I am using sphinx-autodoc to build a html representation of the docstring of the different modules. On my local machine everything works fine, however, since I need to mock PyQt4 on read-the-docs, I ran into the following problem: In one of my GUI classes, I subclass QtGui.QDialog via
class listSelectorDialog(QtGui.QDialog):
def __init__(self,parent,List):
super(listSelectorDialog,self).__init__(parent)
and listSelectorDialog via
class advancedListSelectorDialog(listSelectorDialog):
def __init__(self,parent,List):
super(advancedListSelectorDialog,self).__init__(parent,List)
Mocking QtGui will result in read-the-docs telling me:
class advancedListSelectorDialog(listSelectorDialog):
TypeError: Error when calling the metaclass bases
str() takes at most 1 argument (3 given)
and hence crashing. I've tried to build my package into a virtual environment by selecting
Install your project inside a virtualenv using setup.py install
however, it turns out even though PyQt4 is listed in pip, you can not install it, see https://superuser.com/questions/679298/how-to-install-pyqt4-and-what-are-the-practical-differences-between-pyqt4-and-py.
The only workaround I've found so far is to not load the GUI modules if the environment is RTD and leave out the documentation of the GUI modules, however this should not be the final solution. Thanks.

I had a similar problem with PyQt5/py3 (a metaclass conflict with MagickMock). My workaround is to manually mock the module in conf.py instead of using unittest.mock:
class PyQt5:
#staticmethod
def qVersion():
return '5.0.0'
class QtCore:
class QObject:
pass
# etc...
sys.modules['PyQt5'] = PyQt5
This makes the import / metaclass conflict problem disappear. Unfortunately autodoc still doesn't work (no output), though the builds pass...
Tedious of course.

Related

import Qt ui to Maya2020

Last week I started learning Python for Maya, following the 'CGCircuit - Learn Python Inside Maya'.
Now I'm trying to make a simple UI for Maya2020 using Qt. The tutorial itself is pretty outdated, a lot has changed since Maya2015.
I checked a lot of forums and it seems I’m not the only one having problems. This is what I learned so far:
Qt designer is not a part of Maya anymore, so I downloaded Qt Creator 4.14.0 (Community)
I used pip to install: shiboken2-5.15.2-5.15.2-cp35.cp36.cp37.cp38.cp39-none-win_amd64.whl
If I'm correct, Maya2020 has Pyside2 pr-installed, but I also Installed Pyside2 on Windows10
QtGui.QDialog is replaced by QWidget
Currently I ‘m using Python 2.7.17 on my Windows system, but I also have Python3.8 available in my Environment Variable
When I call the py script inside Maya, like this:
import geomGenerator
reload(geomGenerator)
geomGenerator.getMayaWindow()
geomGenerator.run()
I get this error:
Error: NameError: file C:/Users/12213119/Documents/maya/2020/scripts\geomGenerator.py line 12: global name 'shiboken2' is not defined #
Not sure what this means or how I can solve it. I looked all over the internet, please let me know what I'm doing wrong. Thx in regard.
from PySide2 import QtGui, QtCore, QtUiTools, QtWidgets
from shiboken2 import wrapInstance
import maya.cmds as mc
import maya.OpenMayaUI as omui
def getMayaWindow():
""" pointer to the maya main window
"""
ptr = omui.MQtUtil.mainWindow()
if ptr is not None:
return shiboken2.wrapInstance2(long(ptr), QtWidgets.QWidget)
def run():
""" builds our UI
"""
global win
win = GeometryGenerator(parent=getMayaWindow())
win.show()
class GeometryGenerator(QtWidgets.QWidget):
def __init__(self,parent=None):
super(GeometryGenerator,self).__init__(parent)
I guess you need to run you script with python that comes with Maya, and use pip that comes with maya to install packages.

Using pyinstaller on parmap causes a tkinter matplotlib import error. Why?

Update
If I try downgrading from Pyinstaller 3.2 to 3.1 I instead get the following traceback when I try to run the executable.
I tried adding --hidden-import=collect_submodules('pkg_resources._vendor') to pyinstaller as noted here but it had no effect. Same error. This appears to be due to an issue with setuptools. I'm using 26.0.0. Downgrading to 19.3 that many sources say fixes the issue does indeed fix this issue but then I'm back to the issue I have below.
I have a python 2.7 pyqt4 project I'm trying to turn into an .exe using pyinstaller. I use:
pyinstaller --additional-hooks-rir=. --clean --win-private-assemblies pipegui.py
pipegui.py can be found on github here
I get a working executable and the app appears functional. Here is what the terminal spits while pyinstaller is freezing. However the app crashes when I run particular parts of my program. It crashes and the terminal goes in a loop continually outputting the same traceback below with "Poolworker-X" at the very top continually incrementing:
As you can see tkinter is implicated, despite the word "tkinter" appearing nowhere in my project (using pyqt4). I am making use of matplotlib though and from answers discussed here and here I have added the following to the top of pipegui.py my main script:
from Tkinter import *
import Tkinter
import FileDialog
This however seems to be a step in the wrong direction because after freezing with this (and the same flags as before) my executable wont even open and instead I get this:
Here is pyinstaller's pretty-much identical output while freezeing. Remember all I did was add those 3 import statements above. That's it.
I also tried pyinstaller --additional-hooks=. --clean --win-private-assemblies --hidden-import=Tkinter pipegui.py and it made no difference. I'm completely perplexed as to why trying to import tkinter is doing this. Will fixing this traceback lead me closer to solving the other?
I only figured out after trying all this that the only parts of my executable that are crashing are parts that make use of parmap multiprocessing. Classes that make use of matplotlib but not parmap are working fine.
So please note my question is how the first traceback can be fixed and also why are both matplotlib and tkinter popping up in the traceback despite my code where the crash occurs making use of niether?
Extra notes
I use --clean --win-private-assemblies to fix error code 14001 as per here
Repiklis provided the solution in the comments. Further note that as of January 15 2017 Pyinstaller version 3.2.1 was released. I now use this and it solves this issue along with others like this and this that I could previously only solve by using the developer version. So I highly recommend upgrading to the latest version if you haven't already.

redirecting python import to another module

I'm working on a large open source Python project, which has modules used by both the project and other projects. The goal is to move some of these modules out to a new "library" project that can then be imported by the original project and other projects.
To make this transition smooth, the thought was to copy the modules over to the new project, and have the original project then use the new import. However, to allow other project to have time to migrate later, the thought was to have the original module redirect the import.
For example, the usage is like this in repo 'neutron' (other projects could do the same):
cat neutron/consumer.py
from neutron.redirected import X
print(X)
The in the new 'neutron_lib' project created, the module looks like this (the same as what the original was in project 'neutron'):
cat ../neutron-lib/neutron_lib/redirected.py
X = 5
In the 'neutron' project, I'm trying to do this as the redirect module:
cat neutron/redirected.py
import neutron_lib.redirected
import sys
sys.modules['neutron.redirected'] = neutron_lib.redirected
When I run pylint, it gives these errors:
************* Module neutron.redirected
E: 1, 0: No name 'redirected' in module 'neutron_lib' (no-name-in-module)
************* Module neutron.consumer
E: 1, 0: No name 'X' in module 'neutron.redirected' (no-name-in-module)
If I run this, it runs fine, and consumer.py prints '5'. If I use ipython and load consumer.py, I can see 'X' in dir() output.
Any idea why I'm getting this pylint error? Is it a false error? Is there a way to override it?
Looks like, when running under tox, I can add the following to .pylintrc to hide the errors/warnings
no-name-in-module
nonstandard-exception
When I run pylint it passes now, as does running the Unit tests. Just wish I understood why I'm getting these errors/warnings though.

working with antlr in python

I am trying to use lexer.py and parser.py in python , but it has error in this code :
import antlr3
import antlr3.tree
import traceback
from test22Lexer import test22Lexer
from test22Parser import test22Parser
char_stream = antlr3.ANTLRStringStream("input.txt")
lexer = test22Lexer(char_stream)
tokens = antlr3.CommonTokenStream(lexer)
parser = test22Parser(tokens)
parser.block()
and the error is :
class block_return(ParserRuleReturnScope):
NameError: name 'ParserRuleReturnScope' is not defined
I need help :D
parser :
class block_return(ParserRuleReturnScope):
def __init__(self):
super(test22Parser.block_return, self).__init__()
self.tree = None
I just had the exact same problem. I went through the egg looking for the class (ParserRuleReturnScope), and it did not appear to exist. This was using version 3.0.1. I was then able to find the 3.1.3 runtime, and the associated version of the ANTLR generator JAR file. Once I generated using 3.1.3, the problem went away. So, I would recommend installing 3.1.3, here are the relevant links I used:
ANTLR generator:
https://github.com/antlr/website-antlr3/blob/gh-pages/download/antlr-3.1.3.jar
Download the .jar file, and then run:
java -jar path/to/jar/file myGrammar.g
Once your parser/lexer are generated using 3.1.3, use the same version runtime:
https://github.com/antlr/website-antlr3/blob/gh-pages/download/Python/antlr_python_runtime-3.1.3-py2.5.egg
You can install that, e.g. with easy_install.
Now the module loads (syntax is correct), but I'm still working through runtime errors. Progress!
Honestly, I know nothing of ANTLR, but you should have more faith in Python's error message:
NameError: name 'ParserRuleReturnScope' is not defined
It clearly indicates that it's not defined, so you forgot to include it. As you can see in Python ANTLR page, they suggest including it with a wildcard, like:
from antlr3 import *
which would make it work because, if I'm not mistaken, ParserRuleReturnScope is defined in the antlr3 package. If you don't want to import everything in antlr, you could import it as you do and prefix all objec by the package, like: altlr3.ParserRuleReturnScope.
As I wrote, I know nothing of ANTLR, sorry if it doesn't work, I tried to help :)

py2exe can't find msvcp90.dll

I'm working on converting a simple GUI script written with Python 2.7 and Pyqt4 into a standalone executable using py2exe. I keep getting "no such file exists" errors, and I've managed to fix a few, though this one seems stubborn. It can't find msvcp90.dll, and returns an error message with a short traceback to distutils and then back to my py2exe script, which isn't very enlightening.
I've installed the MS C++ redistributable runtime, as recommended in
py2exe fails to generate an executable
but my script still can't locate the .dll. Below is my py2exe script, with the name of my script blocked out:
from distutils.core import setup
from py2exe.build_exe import py2exe
import sys, os, zmq
sys.argv.append('py2exe')
os.environ["PATH"] = \
os.environ["PATH"] + \
os.path.pathsep + os.path.split(zmq.__file__)[0]
setup(
options = {'py2exe':{'bundle_files':1,"includes":["zmq.utils",
"zmq.utils.jsonapi","zmq.utils.strtypes"]}},
console = [{'script':"#######.py"}],
zipfile = None
)
I've already fixed an issue with zmq (which isn't ever used by my script, or my GUI, for that matter, as far as I know). What am I doing wrong?
Right, I've managed to get my app to build, and although the question is now moderately old, it's my hope this is eventually of use to someone.
Firstly, py2exe is probably the wrong tool. It's old and AFAICT unmaintained. Consider PyInstaller instead. Using PyInstaller is literally as simple as installing it, installing PyWin32, and then going python %path_to_pyinstaller%/pyinstaller.py --onefile --windowed source.py. PyInstaller deals with all the mess of side by side assemblies and so on without you having to do anything.
In short, use PyInstaller.
However, to answer your question, this worked for me:
The question you've linked to - in particular this answer is the right start. Find the right DLLs and copy them to C:\Python27\DLLs
Ditch your existing setup.py file. If you're not using zmq, there's no reason to import it. Also, for a windowed application you want windows= not console=. My file goes (for packaging show.py):
#!/usr/bin/python
from distutils.core import setup
import py2exe
setup(options={'py2exe':{'bundle_files':1}},
windows=['show.py'])
(This is pinched off http://www.blog.pythonlibrary.org/2010/07/31/a-py2exe-tutorial-build-a-binary-series/)