从不同子文件夹的子文件夹中相对导入python模块 [英] Relative importing python module from a subfolder from a different subfolder

查看:631
本文介绍了从不同子文件夹的子文件夹中相对导入python模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用alembic,它是python中的sqlalchemy工具。您键入一个命令,它会生成一个文件夹alembic,里面有py文件。里面的py文件,需要在一个名为myapp的单独文件夹中链接到我的应用程序。但我不能把它联系起来。它说它不存在,相对导入不起作用。

I'm trying to use alembic which is an sqlalchemy tool in python. You type a command and it generates a folder "alembic" with py files inside. The py file inside, needs to link to my application in a separate folder called "myapp". But I cannot link it. It says it doesn't exist and relative import doesn't work.

所以我需要从 myapp / configs / config.py <导入我的配置类/ strong>文件。

so I need to import my config class from myapp/configs/config.py file.

/apps
+--/alembic
|----env.py <--- the calling file
+--/myapp
|----configs/__init__.py <--- has "DefaultConfig" class imported
|----configs/config.py <--- I want to import the class inside here.

在env.py中:

from myapp.configs import DefaultConfig

不起作用。

我试过:

from ..myapp.configs import DefaultConfig

没有成功。

alembic docs中的示例代码说只需使用myapp.whatever。

example code in alembic docs say just use "myapp.whatever".

我甚至在环境变量中将/ apps和/ myapp添加到PYTHON_PATH。

I even added my "/apps" and "/myapp" to PYTHON_PATH in environment variables.

示例错误:

File "D:\apps\myapp\lib\site-packages\alembic\command.p
y", line 97, in revision
    script.run_env()
  File "D:\apps\myapp\lib\site-packages\alembic\script.py
", line 191, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "D:\apps\myapp\lib\site-packages\alembic\util.py",
 line 186, in load_python_file
    module = imp.load_source(module_id, path, open(path, 'rb'))
  File "alembic\env.py", line 5, in <module>
    from ..myapp.configs import DefaultConfig as conf
ValueError: Attempted relative import in non-package


推荐答案

您有两种可能的解决方案:

You have two possible solutions to your problem:

通过在终端中运行以下BASH / SH shell命令,添加应用程序目录的路径:

Add the path to apps catalogue by running following BASH / SH shell commands in your terminal:

$ export PYTHONPATH=$PYTHONPATH:'/path/to/apps'

请注意它到PATH环境变量将无法正常工作。要了解有关PYTHONPATH的更多信息,如何管理它以及一般模块的友好和友好信息:

Please not that adding it to the PATH environment variable won't work. To find out more about PYTHONPATH, how to manage it plus nice and friendly info on modules in general:

http://www.stereoplex.com/blog/understanding-imports-and-pythonpath

但请注意,这种方法确实会影响系统的PYTHONPATH。强烈建议使用virtualenv - 以防万一出错,它不会影响您的所有系统和其他应用程序。使用virtualenvwrapper时:

Please note however that this approach does affect your system's PYTHONPATH. It is highly recommended to use a virtualenv - just in case things go wrong, it won't affect all your system and other apps. When using virtualenvwrapper:

$ add2virtualenv '/path/to/apps'

更多这里

或者,您可以执行相同的操作,但仅适用于脚本运行时添加:

Alternatively, you can do the same but just for a script runtime by adding:

import sys
sys.path.append('/path/to/apps')

到您的 apps / alembic / env.py 文件。

最后,在同一档案中,进行以下更改:

Finally, in same file, make a following change:

from myapp.configs.config import DefaultConfig

请注意您的应用/ myapp 文件夹还应包含 __ init __。py 文件(可能为空),以使Python可以像Demian Brecht所指出的那样处理模块。

And please note that your apps/myapp folder should also contain __init__.py file (might be empty) to make Python to treat is as a module as Demian Brecht pointed out.

这篇关于从不同子文件夹的子文件夹中相对导入python模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆