简体   繁体   English

Python:如何从另一个文件夹导入脚本,而该文件夹又在本地导入具有相对路径/本地文本文件的其他脚本?

[英]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.pyPYTHONPATH驻留的目录。

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的依赖关系, PYTHONPATHsomepackage所在的路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用文件中的相对导入的情况下从不同目录中的 Python 脚本执行文件(导入其他模块)? - How can I execute a file (that imports other modules) from a Python script in a different directory without using relative imports in the file? 在Python中,如何从具有相对路径的祖父母文件夹中导入? - In Python, how can I import from a grandparent folder with a relative path? 如何使用相对路径从不同目录导入python脚本? - How to import python script from a different directory using relative paths? 使用 Python 3 从位于另一个目录中的模块导入本地 function 并在 Jupyter Notebook 中进行相对导入 - Import local function from a module housed in another directory with relative imports in Jupyter Notebook using Python 3 如何制作定义了相对路径的python配置文件,但是当其他目录中的脚本导入配置时,路径正确吗? - How to make python config file, in which relative paths are defined, but when scripts in other directories import config, paths are correct? 如何从 Python 中的相对路径导入自定义函数? - How do I import custom functions from relative paths in Python? 如何创建导入其他导入的python脚本 - How to create a python script that import other imports 如何从python中的另一个文件夹导入文件? - How to Import files from another folder in python? 如何从其他路径导入自写的python文件? - How to import self-written python files from other paths? 如何在不知道路径的情况下从其他相关文件夹导入? - How I can Import from a other relative folder wihout the knowledge of the path?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM