[英]Python: How can i import a script from another folder that also locally imports other scripts with relative paths/local text files?
I have been given a python program that runs on its own. 我得到了一个可以自行运行的python程序。 The structure is such that there one .py file (lets call it app.py ) that imports many other scripts from its nearby folders and uses them t do something.
这样的结构使得存在一个.py文件(我们称其为app.py ),该文件从其附近的文件夹中导入了许多其他脚本,并使用它们来执行某些操作。 Those other modules have local config.txt files and also import other local modules.
这些其他模块具有本地config.txt文件,并且还导入其他本地模块。
I would like to have my own script somewhere else that imports this "app.py" and just call a function from it. 我想在导入该“ app.py”的其他地方有自己的脚本,然后从中调用一个函数。 The problem is: This function i would like to call depends on other modules that are relatively imported to app.py itself and when i try to import app.py to my script, is tells me that i cant find the relative imports that app.py has.
问题是:我想调用的函数取决于相对导入到app.py本身的其他模块,当我尝试将app.py导入到脚本时,是告诉我找不到该应用程序的相对导入。 py有。
I have done quite some digging (with adding __init.py__ files, using system.path.append / insert stuff i am quite familiar by now ) and i found that when i try to run my script it becomes the current working directory and any relative directory imported from app.py - which is imported by my script- can not be found since it is simple not there. 我做了很多挖掘工作(添加__init.py__文件,使用system.path.append /插入我现在很熟悉的东西),我发现当我尝试运行脚本时,它将成为当前工作目录以及任何相对目录找不到从app.py导入的目录(由我的脚本导入),因为它不存在,很简单。
What i can do i, i can import everything with their absolute paths or add every package/folder to the system path but since there more than 10 packages, i dont want to do that. 我可以做什么,我可以使用绝对路径导入所有内容,或将每个包/文件夹添加到系统路径,但是由于有10个以上的包,我不想这样做。 It also would be really problematic if i move the app.py and modules surrounding it.
如果我移动app.py及其周围的模块,这也将确实有问题。
The app.py is very self contained and can be put anywhere. app.py非常独立,可以放在任何地方。 I will appreciate any kind of advice to solve this problem in a nice/elegant way.
我将不胜感激,以任何形式的建议都可以很好/优雅地解决此问题。
TL;DR : I have a script that tries to import another script in another directory but it also imports other scripts with relative paths. TL; DR :我有一个脚本试图在另一个目录中导入另一个脚本,但是它也导入具有相对路径的其他脚本。 The script i try to import gets imported but cant import other scripts it normally imports.
我尝试导入的脚本已导入,但无法导入通常导入的其他脚本。
One variant would be to specify the directory where app.py
resides in the PYTHONPATH
. 一种变体是指定
app.py
在PYTHONPATH
驻留的目录。
Let's assume you have the following files: 假设您具有以下文件:
yourscript.py -> which can be anywhere on the syste, yourscript.py- >可以在系统上的任何位置,
import app
app.dosomething()
somepackage/app.py somepackage / app.py
import somemod
from subpack import innermod
def dosomething():
somemod.x()
othermod.y()
somepackage/somemod.py somepackage / somemod.py
def x():
print("XXX")
somepackage/subpack/innermod.py somepackage / subpack / innermod.py
def y():
print("YYYY")
Now if you would call yourscript
like: 现在,如果您以如下方式调用您的
yourscript
:
PYTHONPATH=<path_to_your_somepackage> python yourscript.py
It will resolve the dependencies in yourscript relative to that PYTHONPATH
which is the path where somepackage
resides. 它将解决脚本中相对于
PYTHONPATH
的依赖关系, PYTHONPATH
是somepackage
所在的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.