pyqt4 + pyyaml/ruamel.yaml dump + pyinstaller bundling breaks application - pyqt4

I'm confused with some specific behaviour and can't find some informations that help me understand the error.
The situation is as follows: I made a small pyqt4 app that at some point dumps an OrderedDict to a yaml string using pyyaml or ruamel.yaml (tried both) and writes this to a file, or reads from this file. This goes very well executing the code as normal. Now I want to distribute my app by bundling it into a single file windows exe using pyinstaller.
Now if I directly use yaml.dump() or ruamel.yaml.dump() in a method of my pyqt4 form class to generate the yaml-string and write to a file (the standard way using with open ...), I am able to bundle the app using pyinstaller and the exe runs fine.
However, if I write a small function in a sub-folder/module that uses the exact same call to pyyaml (yaml.dump(dict)) or ruamel.yaml (ruamel.yaml.dump(dict, Dumper=ruamel.yaml.RoundTripDumper)) to generate the yaml string and save to a file using with open ... and use this in my pyqt4 method (I just wanted to make things more readable), pyinstaller starts to load a bunch of modules and does a lot more stuff (according to console output), resulting in the exe file beeing almost 5 times larger plus unusable throwing a fatal error pyi_rth_pkgres returned -1 at start.
Unfortunately, I don't understand much from either console output or warnings log, viewable in this gist. Maybe I am searching for the wrong terms. I also tried renaming the module to prevent shadowing.
Now my question is, does anybody know whats going on and can explain this behaviour?

After doing a lot of trial and error, I finally got it working.
I created a new module and build the dumping functions inside it. pyinstaller and the bundled exe work flawless. However, if i do the exact same thing in the previous module, even refactoring the name, it does not work. I even copied the complete code to the old module and it doesn't work. I have no idea why and at this point I am too afraid to ask :|
I am just glad it works now.

Related

How do I force VS Code to import the latest Python source file?

I am working with Python in VS Code. I have two modules, mod_A and mod_b, where mod_A imports a method do_this_thing from mod_B. Everything was going fine until I tried to refactor do_this_thing into _do_this_thing. (The function is an internal function).
I used the Rename Symbol option in VS Code to perform the refactor. I doublechecked that all instances in my fairly simple code base were covered.
The strange behavior that I'm getting is that when I run mod_A and it gets to the renamed function call, I get an AttributeError telling me that there is no function _do_this_thing. The head scratcher, though, is that if I manually change _do_this_thing back into do_this_thing in mod_A, my code executes as expected— even though it is still defined as _do_this_thing in mod_B! Furthermore, I can't step into do_this_thing when I run it in mod_A.
I have tried deleting all __pycache__ files, as that was the first thing that I thought of. I've also tried clearing VS Code's cache, deleting my project, and starting over with the refactor from a fresh pull from GitHub. No dice.
Hopeful that someone here can help me solve this esoteric problem!
Edit: Adding some debugging context for the votes to close my question:
Windows 10, VS Code
Miniconda Python distribution
PYTHONPATH variable has project directory on it
Modules imported correctly; the bug happened when I changed the name of a function in the module being imported
Python appears to be importing a cached verison of the earlier code from somewhere, but I've reset everything and still have the issue

Freeling Python API working on sample, get Import error on other code

I'm trying out Freeling's API for python. The installation and test were ok, they provide a sample.py file that works perfectly (I've played around a little bit with it and it works).
So I was trying to use it on some other python code I have, in a different folder (I'm kind of guessing this is a path issue), but whenever I import freeling (like it shows on the sample.py):
import freeling
FREELINGDIR = "/usr/local";
DATA = FREELINGDIR+"/share/freeling/";
LANG="es";
freeling.util_init_locale("default");
I get this error:
ModuleNotFoundError: No module named 'freeling'.
The sample.py is located on the ~/Freeling-4.0/APIs/Python/ folder, while my other file is located in ~/project/, I dont know if that can be an issue.
Thank you!
A simple solution is to have a copy of freeling.py in the same directory as your code, since python will look there.
A better solution is to either paste it in one of the locations where it usually checks (like the lib folder in its install directory), or to tell it that the path where your file is should be scanned for a module.
You can check out this question to see how it can be done on Windows. You are basically just setting the PYTHONPATH environment variable, and there will only be minor differences in how to do so for other OSes. This page gives instructions that should work on Linux systems.
I like this answer since it adds the path at runtime in the script itself, doesn't make persistent changes, and is largely independent of the underlying OS (apart from the fact that you need to use the appropriate module path of course).
You need to set PYTHONPATH so python can find the modules if they are not in the same folder.

I am unable to access imported models attribute

import pocketsphinx
for phrase in pocketsphinx.LiveSpeech():print(phrase)
So the weirdest thing ever happened. This code worked fine. Then I cut and pasted it into another python file in the same project and boom I get, AttributeError: module 'pocketsphinx' has no attribute 'LiveSpeech'. pocketsphinx is in my external libraries and my ide recognizes it, but now there is no autocomplete options with pocketsphinx.(should have recommendations). So this is weird. When I cut and past back to old file it does not work either now. ??? Hmm... Why
This kind of error often happens when there is a module in the project directory with the same name as the one being imported. This file is then found earlier when traversing sys.path, so it shadows the library you're trying to import.
So in this case you probably have a file pocketsphinx.py within your project directory. If you rename that to something else then it should work.

How to reload programatically a module contained within a package in python 3.2

So far I've seen the answer for Python2 however it doesn't work on Python3, I want to be able to always get the latest changes in a module that lives in a package every time the code runs without reopening a new interpreter every time. Since modules seems to be loaded just once for performance purposes as specified in documentation,I would like to be able to force a load in the modules programatically right before starting my program. Thanks in advance...
Although I'm not a fan of answering my own questions, I think in this case totally worth to mention it since seems to be quiet useful, in order to reload a module that you previously modified without having to restart the whole interpreter, just programatically forcing the modules (contained within a package) to get the latest changes this is the way to go:
import com.your.package.YourModule as MyModuleInPackage
import imp
imp.reload(MyModuleInPackage)
Notice that trying to use imp.reload(com.your.package.YourModule) causes an error, so the way to go is by having an Alias for the fully quialified name of the module and use it in the reload function to work properly...
Hope this helps.
Regards!

how to distribute a Flash component for use with MTASC?

I have a Flash component that's just a library of compiled code with some exposed API calls. Normally we distribute this as a SWC or MXP, and it works just fine. Recently I had a client express interest in using my component, but they do all their development in MTASC only. MTASC doesn't support SWC files, so ss there a good way to send precompiled code that would work in MTASC? I'm not able to send them the original source code, but if there's some other method I'd appreciate it. I do have access to the source, so I can recompile it however necessary. Thanks!
I did find an answer, and I'm not 100% sure if this is exactly the process since I'm no longer at that job and don't have the computer/process in front of me anymore. It was a bit of a hack.
What it involved basically was unzipping the SWC file and getting a .swf and a bunch of .asi files out.
The .asi files are really just ActionScript files, but they contain intrinsic definitions, or just prototypes or footprints of whats actually there. The real meat of it is still in the .swf.
So you rename all those .asi files to .as and then put them into your MTASC classpath. Since they contain definitions, you shouldn't be getting any more "undefined variable" or "undefined function" errors at compile time. Now you just need to pull in the SWF, where the actual function bodies are defined, using loadMovie. once the loadMovie is complete, you should be able to use all of the functions.
The only caveat of course is that you have to wait for that SWF to load before calling of any of the functions from the SWC.
so step-by-step, it looks like this:
1.) unzip the SWC file. this can be done using WinZip or OS X terminal unzip command
2.) Rename .asi files to .as
3.) add new .as files to MTASC classpath
4.) add AS code to load the .swf in and make sure none of the SWC functions are called before the SWF is loaded
5.) compile
I'm pretty sure this is what we did, but i'm not in a spot to try it out right now.,
Hope this helps, let me know if you have any other questions and I'll see if I can help figure it out any more.

Resources