简体   繁体   English

如何从同级目录导入python模块?

[英]How to import python modules from sibling directory?

I have a python (3.6.3) project which has the following structure 我有一个具有以下结构的python(3.6.3)项目

setup.py
utils #python package
utils/constants.py
utils/name_reports.py
normalization #python package
normalization/resolve_name.py
categorization #python package
categorization/animal_categories.py

You can find the source code here on github - https://github.com/techmango-org/python-import-resolution 您可以在github上找到源代码-https: //github.com/techmango-org/python-import-resolution

Here is the source code for utils/name_reports.py 这是utils/name_reports.py的源代码

import normalization.resolve_name as rn
import categorization.animal_categories as ac

test_name = 'DOGY'
resolved_name = rn.get_animal_name(test_name)

print('********ANIMAL CATEGORY IS:', ac.get_animal_score(resolved_name))

When I run python utils/name_reports.py I am getting the following exception 当我运行python utils/name_reports.py时,出现以下异常

Traceback (most recent call last):
  File "utils/name_reports.py", line 1, in <module>
    import normalization.resolve_name as rn
ModuleNotFoundError: No module named 'normalization'

I have tried to solve this problem by installing the current package into virtual env site packages by running pip install . 我试图通过运行pip install .将当前软件包安装到虚拟环境站点软件包中来解决此问题pip install . but that means for every local change I have to run pip install --upgrade . 但这意味着对于每个本地更改,我都必须运行pip install --upgrade . to move my local changes into site packages. 将我的本地更改移到站点包中。

I have been using a hack -m unittest to get this problem solved. 我一直在使用hack -m unittest来解决此问题。 Check this screenshot 查看此屏幕截图

在此处输入图片说明

But I am unable to understand what difference does it create. 但是我无法理解它会带来什么不同。 Here are the exact questions - 这是确切的问题-

  1. How to resolve python import issue in this situation? 在这种情况下如何解决python导入问题?
  2. Is there a better approach to structure python code such that we do not face relative imports issue? 有没有更好的方法来构造python代码,这样我们就不会遇到相对导入的问题?
  3. What difference does -m unittest is creating which is solving this issue? -m unittest在解决这个问题上有什么区别?

Since there are already many answers on SO for this*, I will focus on question (2). 由于SO *已经有很多答案,因此我将重点关注问题(2)。 About what is a better code organization: 关于什么是更好的代码组织:

|- setup.py
|- top_pkg_name
    |- __init__.py
    |- pkg1
        |- __init__.py
    |- pkg2
        |- __init__.py

The (relative) import is done as follows, from inside module_2 : (相对)导入是从module_2内部完成的,如下所示:

from ..pkg1 import module1 as m1

Alternatively, you can use absolute imports, which refer to the top package name: 另外,您可以使用绝对导入,这是指顶级软件包名称:

from top_pkg_name.pkg1 import module1 as m1

In such an organization, when you want to run any module as a script, you have to use the -m flag: 在这样的组织中,当您想将任何模块作为脚本运行时,必须使用-m标志:

python -m top_pkg_name.pkg1.module1

For question (3), I'm not sure but it looks like the unittest module adds the folder to path, allowing the imports to happen directly. 对于问题(3),我不确定,但是看起来好像unittest模块将文件夹添加到路径,从而允许直接进行导入。

*Refer: *参考:

  1. How do I import a Python script from a sibling directory? 如何从同级目录导入Python脚本?
  2. Python import module from sibling folder 兄弟文件夹中的Python导入模块

Use Import statements: 使用导入语句:

from normalization import resolve_name
from categorization import animal_categories

Your Code: 您的代码:

from normalization import resolve_name as rn
from categorization import animal_categories as ac

test_name = 'DOGY'
resolved_name = rn.get_animal_name(test_name)

print('********ANIMAL CATEGORY IS:', ac.get_animal_score(resolved_name))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM