55806

py2exe cannot import Module from other directory

I am bundling python source code with py2exe. The directory structure is as follows:

some_Mod.py some_dir/another_dir/some_Mod.py

Inside the latter some_dir/another_dir/some_Mod.py I am trying to import the other Python Module with

from ..some_Mod import *

Using the import causes no problems with the python interpreter, but if I run the same constellation in the bundled package, I get an Exception:

ImportError: No module named some_Mod

Can somebody explain why?

Remark: Renaming the Modules is actually no problem, but I was just wondering, why py2exe cannot deal with this constellation.

Answer1:

If you have __init__.py files in each of those sub-directories then all import statements should work correctly. Assuming that's not the problem, here's an excellent guide to importing best practices:

http://blog.habnab.it/blog/2013/07/21/python-packages-and-you/

In summary, never use relative imports - always absolute (see the link above for why).

Second (and I'm not entirely sure why), always keep your py2exe setup.py script in the exact folder where your main script is. I've tried modifying py2exe's 'script' option to allow my script to be somewhere else... but your exact problem happened to me. So, try making sure it is right where the main script is.

Finally, you can always give py2exe a little help. I usually have to add the root directory to the system path so the import statements are valid. Note, I'm not modifying sys.path in any of my application's code - only the py2exe script I use to build the exe.

At the top of my py2exe setup script:

import sys sys.path.append(PATH_WHERE_PACKAGES_ARE) # add any packages that need explicit importing here located in root directory: import package1 # apparently it wasn't found... import package2 # apparently same thing

Generally I don't import packages though, adding the project root where they exist usually is enough.

Answer2:

I'm not sure that py2exe now how to handle the from ..some_Mod import * syntax, check this to ensure that the some_Mod.py module is correctly packaged : python -m py2exe.mf -d some_dir/another_dir/some_Mod.py as explained in the py2exe FAQ

Recommend

  • The difference between type and newtype in Haskell
  • Changing xib name [duplicate]
  • Qt load indicator by animated image (aka preloader) or alternative?
  • Renaming NativeScript iOS Application
  • Development oriented PostgreSQL IDE
  • Check for instance of Python multiprocessing.Connection?
  • How to enable autocomplete when using Internet Explorer and SSL?
  • Cannot end a section without first starting one in Laravel
  • How to debug charts package 'No module named 'plot' ' Error
  • Wait for angular to finish updating the DOM
  • Design of Service Layer and Repositories in Microsoft MVC
  • Error while importing scikits.talkbox
  • Redirect to trailng slash (htaccess)
  • Python Tkinter after event OOPS implementation
  • Many to Many in Linq using Dapper
  • MRO with multiple inheritance in python
  • what is the purpose of “export as namespace foo”?
  • What is this strange character in chrome's resource css viewer?
  • How to repeat sections of a SQL query across UNIONs? (DRY in SQL)
  • How can I prevent the need to copy strings passed to a avr-gcc C++ constructor?
  • ImportError: cannot import name Pubnub
  • Faster Way To Simultaneously Iterate Over Rolling Window Of Two Or More Numpy Arrays?
  • Why are YouTube videos using 'youtube.com/v' not loading
  • What does “t” refer to in this SQL?
  • CERN ROOT exporting data to plain text
  • Combining two different ActiveRecord collections into one
  • error importing numpy
  • Django simple Captcha “No module named fields” error
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Is there a perl module to validate passwords stored in “{crypt}hashedpassword” “{ssha}hashedpassword
  • Why does access(2) check for real and not effective UID?
  • Meteor helpers not available in Angular template
  • Join two tables and save into third-sql
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • Google cloud sdk not working when python points python3
  • Python: how to group similar lists together in a list of lists?
  • using HTMLImports.whenReady not working in chrome
  • Understanding cpu registers
  • Converting MP3 duration time