py2app and xml.etree.ElementTree - python

I'm trying to build an app that uses some xml data using Python's built-in xml.etree.ElementTree class. It works properly when I run from the command line, but when I build it, I get an error "ImportError: No module etree.ElementTree." I'm guessing this is because I'm not importing that module correctly, but I haven't been able to figure out how. When I use the "includes" or "packages" directive, py2app complains with the same error, and when I specifically specify the package_dir (/System/Library/...), it compiles, but still gives me the error. I've included a short example to illustrate the issue.
macxml.py
from xml.etree.ElementTree import ElementTree
if __name__ == '__main__':
tree = ElementTree()
print tree.parse('lib.xml')
This should print out "< Element Library at xxxxxx>" where Library is the root name.
setup.py
from setuptools import setup
setup(name="Mac XML Test",
app=['macxml.py'],
)
What is the correct way to make the mac app utilize this library?
Python 2.6.4
Mac OS X 10.6.2
Edit: I also tried this on another mac (PPC 10.5.8) with Python 2.6.2 and achieved the same results.

After reinstalling and updating macholib, modulegraph, py2app, and setuptools to no avail, I did a little more digging and found the following error in the modulegraph module:
graphmodule.find_modules.find_modules(includes=['xml.etree'])
Traceback (most recent call last):
File "<stdin>", line 1 in <module>
File ".../modulegraph/find_modules.py", line 255 in find_modules
File ".../modulegraph/find_modules.py", line 182 in find_needed_modules
File ".../modulegraph/modulegraph.py", line 401 in import_hook
File ".../modulegraph/modulegraph.py", line 464 in load_tail
ImportError: No module named xml.etree
So I looked more into the load_tail and import_hook functions and found that for some reason it was importing the xml package correctly, but then went to an old install of _xmlplus to look for the etree subpackage (which of course it couldn't find). Removing the _xmlplus package eliminated the error and I was able to get the application to work with the following setup.py file:
from setuptools import setup
import xml.etree.ElementTree
setup(name="Mac XML Test",
app=['macxml.py'],
options={'py2app': {'includes': ['xml.etree.ElementTree']}},
data_files=[('', ['lib.xml'])]
)
The output shows up in the console.

Since the comment doesn't have good formating,
In find_modules.py I changed
REPLACEPACKAGES = {
'_xmlplus': 'xml',
}
To
REPLACEPACKAGES = {
#'_xmlplus': 'xml',
}
And the xml import worked.

If you're using macports (or fink etc.) make sure that py2app is using the correct interpreter. You may have to install a new version to work with 2.6.x (on OS X 10.5, py2app uses 2.4.x).
If that doesn't work my steps for working through path problems are:
Start up the python interpreter that your code (or py2app) uses (are you absolutely certain?? try which python)
import sys; print sys.path
If step 2. gives you a path in /System/Library..someotherpythonversion your code is running in the wrong interpreter.

Related

Py2App error: ModuleNotFoundError: No module named 'cmath' when using Pandas

I am trying to build a standalone app that utilises Pandas. This is my setup.py file:
from setuptools import setup
APP = ['MyApp.py']
DATA_FILES = ['full path to/chromedriver']
PKGS = ['pandas','matplotlib','selenium','xlrd']
OPTIONS = {'packages': PKGS, 'iconfile': 'MyApp_icon.icns'}
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app','pandas','matplotlib','selenium','xlrd'],
)
The making of the *.app file goes smoothly, but when I try to run it, it gives me the following error:
...
import pandas._libs.testing as _testing
File "pandas/_libs/testing.pyx", line 1, in init pandas._libs.testing
ModuleNotFoundError: No module named 'cmath'
I tried to include ‘cmath’ in my list of PKGS and in setup_requires in the setup.py file, but when I tried to build the app using py2app it gave me the error:
distutils.errors.DistutilsError: Could not find suitable distribution for Requirement.parse('cmath')
I am stuck. I couldn't find anything useful online. cmath should be automatically included from what I have been reading. Any ideas on where is the problem and how can I fix it?
I think I have found a solution: downgrade to Python version 3.6.6.
See: python 3.6 module 'cmath' is not found
To uninstall Python I followed this process: https://www.macupdate.com/app/mac/5880/python/uninstall
Then I installed Python 3.6.6: https://www.python.org/downloads/release/python-366/
With Python 3.6.6, Py2App seem to work no problem and includes Pandas smoothly.
It seems that for some reasons cmath is not included in the latest versions of Python? I might be wrong. Please let me know what you think and if you have any questions.
P.S.: I am using MacOS (Mojave 10.14.6) and PyCharm.
I had a similar issue with py2app and cmath. I solved this by adding import cmath into the main script. (MyApp.py in your case) I think doing so may have the modulegraph to add the cmath library files.

pdoc can't import the function from other module

i'm using python 2.7 and trying to gather documentation for our testing project using pdoc.
pdoc is located here: D:\dev\Python27\Scripts
the regression project here: C:\views\md_LDB_RegressionTests_v03.1_laptop\mts\Tests\LDB\Regression\Tests
We are using proboscis for our tests and i'm trying to create html documentation for the separate group of tests, a separate python file in my case.
I run such command:
D:\dev\Python27\Scripts>python pdoc --html "C:\views\md_LDB_RegressionTests_v03.
1_laptop\mts\Tests\LDB\Regression\Tests\tests\check_system_management\check_capa
bilities_encoding_problems.py"
and get such answer:
Traceback (most recent call last):
File "pdoc", line 458, in <module>
module = imp.load_source('__pdoc_file_module__', fp, f)
File "C:\views\md_LDB_RegressionTests_v03.1_laptop\mts\Tests\LDB\Regression\Te
sts\tests\check_system_management\check_capabilities_encoding_problems.py", line
4, in <module>
from common.builders.system_request import default_create_system, create_cap
ability
ImportError: No module named common.builders.system_request
pdoc can't import the function from other modules in our regression...
The structure of our project looks like this:
-Tests (C:\views\md_LDB_RegressionTests_v03.1_laptop\mts\Tests\LDB\Regression\Tests)
-"common" package (with init file)
-"builders" packege
-system_request.py
-"test" package
-check_system_management package
-check_capabilities_encoding_problems.py - this is the file i want to get documentation to
Of course there are lots of other packages but im not sure if it makes sense to describe all the structure now
The import part of the check_capabilities_encoding_problems.py looks like this:
import urllib
from hamcrest import assert_that, all_of
from proboscis import test, before_class, after_class
from common.builders.system_request import default_create_system, create_capability
from common.entity.LDBChecks import LDBChecks
How can i point to pdoc where to look for the functions of other modules?
thank you!
You can set PYTHONPATH env variable. This is a path that say python where to find modules and packages by 3th party also you.
When using pdoc with my Spyder IDE, I use the following script to add a directory to pdoc path
import pdoc
libpath = r'C:\Path\To\Module'
pdoc.import_path.append(libpath)
mod = pdoc.import_module('ModuleName')
doc = pdoc.Module(mod)
string = doc.html()
The pdoc.import_path is a list of currently used paths to look for your module; pdoc.import_path equals sys.path in default. More info can be found in pdoc documentation.
pydoc and pdoc read your code!!!
if you will run it from the same directory pdoc3 --html . or pydoc -w . it should work if all the modules are in the same directory. but if they are not:
make sure your main module in each directory has it sys full path append to it (to the same directory).
sys.path.append("D:/Coding/project/....)
Relative path will not do the trick!

py2exe doesn't find all dependencies

I am trying to create an exe from python code. I can run the code just fine from the command line like this:
python myScript.py
I have installed py2exe from here: http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/
And, have a setup.py that looks like this:
from distutils.core import setup
import py2exe
setup(console=['myScript.py'])
And, I run the setup.py like this:
python setup.py py2exe
I get the following in the output:
The following modules appear to be missing
['Carbon', 'Carbon.Files', '__pypy__', '_scproxy', 'http_parser.http', 'http_parser.reader', 'jinja2._debugsupport', 'jinja2._markupsafe._speedups',
'jinja2.debugrenderer', 'markupsafe', 'pretty', 'socketpool', 'socketpool.util']
And, sure enough, if I try to run the exe, I get errors:
$ ./myScript.exe
Traceback (most recent call last):
File "restkit\__init__.pyc", line 9, in <module>
File "restkit\conn.pyc", line 14, in <module>
ImportError: No module named socketpool
Traceback (most recent call last):
File "myScript.py", line 12, in <module>
ImportError: cannot import name Resource
What do I need to do to get py2exe to find the dependencies?
Thanks
Carbon is the name of two different Mac-specific things.
First, in 2.x, on both Mac OS X and Mac Classic builds, has a Carbon package in the standard library, used for calling Carbon/Toolbox APIs (and, in OS X, CoreFoundation and friends).
Second, in both 2.x and 3.x, on Mac OS X, with PyObjC, the PyObjC wrapper around Carbon.Framework is named Carbon. (PyObjC isn't part of the stdlib, but it does come with Apple builds of Python, and most third-party builds besides python.org's official installers.)
Neither of these will exist on Windows.
py2exe tries to be smart and only import things relevant to your platform. However, it's pretty easy to fool. For example, something like this:
try:
import Carbon.Files
except:
Carbon = collections.namedtuple('Carbon', 'Files')
Carbon.Files = None
if Carbon.Files:
Carbon.Files.whatever(…)
… might make py2exe think Carbon.Files is required.
Now, obviously this isn't your whole problem, but it is a very big red flag that py2exe's module dependency code is not working for your project. You probably have similar problems with all kinds of other modules, so it's both missing some things you need and demanding some things you don't have, and this is probably what's causing your actual problems.
As the FAQ explains, you can debug this by running the module-finder code to see where it's going wrong, like this:
python -m py2exe.mf -d path/to/my_file.py
You could use this information to guide the module-finder code, or to rewrite your code so you don't confuse py2exe.
Or, more simply, just explicitly include and exclude modules in your setup.py as a workaround, without worrying about why they're getting incorrectly detected.
py2exe is python version dependent. Everything you're doing seems to be correct, I would guess you have the wrong version installed.

PyXML on Ubuntu

I just completed a fresh install of Ubuntu 10.10 and I'm trying to run some scripts that use xml and xpath. I get an error from inside PyXML.
I think this is an install error. To get this installed I did the following:
prompt> sudo apt-get install python2.6-dev # The next line wouldn't install without this.
prompt> sudo easy_install PyXML
-------BEGIN ERROR---------
username#ubuntu:~/data/code$ MyScript.py
Traceback (most recent call last):
File "/home/username/data/code/app/trunk/MyScript.py", line 17, in <module>
from xml import xpath
File "/usr/local/lib/python2.6/dist-packages/PyXML-0.8.4-py2.6-linux-i686.egg/_xmlplus/xpath/__init__.py", line 112, in <module>
from pyxpath import ExprParserFactory
File "/usr/local/lib/python2.6/dist-packages/PyXML-0.8.4-py2.6-linux-i686.egg/_xmlplus/xpath/pyxpath.py", line 59, in <module>
from xml.xpath.ParsedAbbreviatedRelativeLocationPath import ParsedAbbreviatedRelativeLocationPath
File "/usr/local/lib/python2.6/dist-packages/PyXML-0.8.4-py2.6-linux-i686.egg/_xmlplus/xpath/ParsedAbbreviatedRelativeLocationPath.py", line 31
as = ParsedAxisSpecifier.ParsedAxisSpecifier('descendant-or-self')
^
SyntaxError: invalid syntax
-------------END ERROR-------------------
I'm about at my limits with PyXML. I simply want to read an xml file and read/write data with xpath. Is there a simpler library that will easily work out of the box? Or any ideas on how to fix this?
Just for the record, if you really need PyXML (i.e. legacy code that you don't have the time to port right now), just changing as in the two places it is used to some other variable name will do.
Additionally, I noticed that Gentoo added the method use_pyxml() to PyXML which explicitly needs to be called; so the standard library XML modules are not used. See here if that is of interest.
PyXML should have been written for very old version of Python (< 2.4) and it used one of the later keywords 'as' as its variable. If your requirement is simple, you can just use ElementTree from Python Standard library which has support for XPath expressions. An example is here.
For using the standard library module, do:
from xml.etree.ElementTree import ElementTree

Py2exe - win32api.pyc ImportError DLL load failed

I am trying to use py2exe to distribute a python application I have written. Everything seems to go OK, but when I run it on another machine it fails with the following error:
Traceback (most recent call last):
File "application.py", line 12, in <module>
File "win32api.pyc", line 12, in <module>
File "win32api.pyc", line 10, in __load
ImportError: DLL load failed: The specified procedure could not be found.
I have googled for this and not found very much, but have tried the following suggestions to no avail:
Imported pywintypes and pythoncom before win32api (in the setup.py for py2exe and in the main application)
Added some code to the setup.py -
# ModuleFinder can't handle runtime changes to __path__, but win32com uses them
import pywintypes
import pythoncom
import win32api
try:
# if this doesn't work, try import modulefinder
import py2exe.mf as modulefinder
import win32com
for p in win32com.__path__[1:]:
modulefinder.AddPackagePath("win32com", p)
for extra in ["win32com.shell"]: #,"win32com.mapi"
__import__(extra)
m = sys.modules[extra]
for p in m.__path__[1:]:
modulefinder.AddPackagePath(extra, p)
except ImportError:
# no build path setup, no worries.
pass
I'm quite new to all this, so any help would be greatly appreciated
Thanks
Jon
I've seen this problem when the package was built on Vista but executed on XP. The problem turned out to be that py2exe mistakenly added powrprof.dll and mswsock.dll to the package. Windows XP contains its own copies of these files though, and can't load the Vista ones which got installed with your app.
Removing them from the package did the trick, you can do this easy by adding this to the options dict in setup.py
'dll_excludes': [ "mswsock.dll", "powrprof.dll" ]
#Wim, I found the bit about "adding this to the options dict in setup.py" a bit confusing. If like me you did not have an options arg in your existing call to setup this might make things clearer:
setup(name='myprog',
...
options={"py2exe":{"dll_excludes":[ "mswsock.dll", "powrprof.dll" ]}},
...
)
Try adding win32api to your packages, in the options dictionary.
Here's an example:
excludes = ["pywin", "pywin.debugger"] # there will be more in real life...
options = dict(optimize=2,
dist_dir="build",
excludes=excludes,
packages=["win32api"])
setup(
name="MyCoolApp",
options=dict(py2exe=options),
# etc ...
Just as an added comment. When rebuilding your program with Py2exe be sure to delete the old "dist" directory. I was sitting for over 3 hours not understanding why my app was working on my dev envirnoment and not in production. deleted dist and rebuild with py2exe and it worked.

Categories

Resources