tags:

views:

36

answers:

1

I have an executable file, named, for example, resource.py. It is disposed in the folder named resource. Renaming file or folder is objectionable - it is old product and users accustomed to use such naming. At old version it was under another project, so imports to it sound like import main_product.resource.<..>.

Now I need to use imports like import resource.<..> and __import__('resource.<..>', ...). Then I try it, I get an expected error, because Python try to import from the file resource.py first.

Suggestion solutions:

  1. remove path of current folder from sys.path:
    • we need an ugly chunk of code at the begin of the file;
    • no relative imports will be available in this folder;
  2. put folder path at the end sys.path list:
    • bigger chunk of ugly code at the begin of the file;
    • i am not sure that this feature will always work perfect.

Have any usable idea about this?

P.S. Python v2.6, FreeBSD 7.3

+1  A: 

Use imp.find_module() and imp.load_module()

find_module lets you specify your own path to search

gnibbler
It may help me in some cases, thanks.
Roman Bodnarchuk