1

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?

asked Jan 17, 2014 at 23:58

1 Answer 1

1

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!

answered Jan 18, 2014 at 4:03
Sign up to request clarification or add additional context in comments.

3 Comments

I understand what you are saying, but maybe I don't because I still do not understand why my solution won't work in that case. I change the current working directory to file, so it is at the place of the module, and then I try to use a relative path from the first module to the second. It works when I use an absolute path. Lastly, what could I do to fix it? I am starting to understand why my program is not working but now I'm even more confused as to how to get it working.
When appending a new path in your QueryDDEX.py file you should take care that you are at the test.py path, so maybe the path you have provided is wrong.
Turns out I was one directory off in my relative path, adding a "/../" solved the problem. Thank you for your help, it would have taken much longer to debug without you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.