Importing module from relative path
Question: I'm looking for some advice on a python issue I am having. I am a novice at
python. I believe that I am relying on my programming experience from other
languages to make this work and I have finally come to a stand-still. Here is
the scenario, I am importing a module that relies on another module.

My driver for the program, called test.py, starts out like this:

    
    
    import sys
    sys.path.append(r'C:\Program Files (x86)\Zorba XQuery Processor 2.9.1\share\zorba\uris\com\nuemeta\www\modules\DDEXpedite\bindings\Python\Code and Other Files')
    
    import QueryDDEX
    

Then in the QueryDDEX.py file I have:

    
    
    import sys,os
    
    temp = os.getcwd()
    os.chdir(os.path.dirname(os.path.realpath(__file__)))
    sys.path.append(os.path.realpath("..\..\..\..\..\..\..\..\..\python"))
    
    print sys.path
    
    import zorba_api
    
    os.chdir(temp)
    

In my head I was thinking (1)Save the current working directory, (2)Change the
current working directory to the directory of the QueryDDEX.py module, (3)
import the zorba_api module from a relative path because if I deploy this
module to other computers they may not have the same file structure as mine,
and (4) change the current working directory back to what it was initially.

Now, I have read that it is not okay to use relative paths and I have also
read that it is okay. I do not see another choice because I did not write the
zorba_api so I do not have too much control over it. Anyway, the output of the
program is this:

    
    
    ['C:\\Users\\Administrator\\Desktop', 'C:\\Python27\\Lib\\idlelib', 'C:\\Windows\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Program Files (x86)\\Zorba XQuery Processor 2.9.1\\share\\zorba\\uris\\com\\nuemeta\\www\\modules\\DDEXpedite\\bindings\\Python\\Code and Other Files', 'C:\\Program Files (x86)\\Zorba XQuery Processor 2.9.1\\share\\zorba\\python']
    
    Traceback (most recent call last):
      File "C:\Users\Administrator\Desktop\Test.py", line 4, in <module>
    import QueryDDEX
      File "C:\Program Files (x86)\Zorba XQuery Processor 2.9.1\share\zorba\uris\com\nuemeta\www\modules\DDEXpedite\bindings\Python\Code and Other Files\QueryDDEX.py", line 9, in <module>
        import zorba_api
    ImportError: No module named zorba_api
    

This is where things get tricky in my opinion, the zorba_api module is located
at

    
    
    C:\\Program Files (x86)\\Zorba XQuery Processor 2.9.1\\share\\zorba\\python
    

and we can see by my debug statement that it IS in the python class path. So
why am I getting this error?


Answer: Check out this scenario. You have the file `alpha.py` at `C:\projects\test\`.
Then you also have a file called `beta.py` at `C:\projects\test\modules\` so
to import `beta` from `alpha` you should do:

    
    
    import modules.beta
    

Or, not very good but useful, adding the `modules` directory to your
`sys.path`.

Then if you want to import modules from your `beta.py` file you will have to
take care that you're not at `C:\projects\test\modules\` directory, you're in
the `alpha.py` directory. So for example, if there's a third file called
`gamma.py` at `modules/`, if you want to import it from `beta.py` you should
use:

    
    
    import modules.gamma
    

Because you are at the _importer file_ (`alpha.py`) path, not at the _imported
file_ (`beta.py`) path.

Hope it helps to solve your problem!

