[英]Importing module from relative path
I'm looking for some advice on a python issue I am having. 我正在寻找有关python问题的一些建议。 I am a novice at python. 我是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: 我的程序驱动程序test.py如下所示启动:
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: 然后在QueryDDEX.py文件中,我有:
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. 我脑子里在想(1)保存当前工作目录,(2)将当前工作目录更改为QueryDDEX.py模块的目录,(3)从相对路径导入zorba_api模块,因为如果我部署此模块到其他计算机,它们可能与我的计算机没有相同的文件结构,并且(4)将当前工作目录更改回最初的目录。
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. 我没有看到其他选择,因为我没有编写zorba_api,因此对它没有太多的控制权。 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 我认为这是棘手的地方,zorba_api模块位于
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. 我们可以通过我的debug语句看到它在python类路径中。 So why am I getting this error? 那我为什么会收到这个错误?
Check out this scenario. 看看这种情况。 You have the file alpha.py
at C:\\projects\\test\\
. 您在C:\\projects\\test\\
有文件alpha.py
。 Then you also have a file called beta.py
at C:\\projects\\test\\modules\\
so to import beta
from alpha
you should do: 然后,您在C:\\projects\\test\\modules\\
也有一个名为beta.py
的文件,因此要从alpha
导入beta
,您应该执行以下操作:
import modules.beta
Or, not very good but useful, adding the modules
directory to your sys.path
. 或者,不是很好,但很有用,将modules
目录添加到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. 然后,如果要从beta.py
文件导入模块,则必须注意您不在C:\\projects\\test\\modules\\
目录中,而是位于alpha.py
目录中。 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: 因此,例如,如果在modules/
处有第三个文件gamma.py
,如果要从beta.py
导入,则应使用:
import modules.gamma
Because you are at the importer file ( alpha.py
) path, not at the imported file ( beta.py
) path. 因为您位于导入文件 ( alpha.py
)路径,而不是位于导入文件 ( beta.py
)路径。
Hope it helps to solve your problem! 希望它能帮助您解决问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.