在嵌套包中导入模块

时间:2022-08-22 08:53:38

This is a python newbie question:

这是一个python新手问题:

I have the following directory structure:

我有以下目录结构:

test
 -- test_file.py
a
 -- b
   -- module.py    

where test, a and b are folders. Both test and a are on the same level.

其中test,a和b是文件夹。测试和a都处于同一水平。

module.py has a class called shape, and I want to instantiate an instance of it in test_file.py. How can I do so?

module.py有一个名为shape的类,我想在test_file.py中实例化它的一个实例。我怎么能这样做?

I have tried:

我努力了:

from a.b import module

but I got:

但我得到了:

ImportError: No module named a.b

3 个解决方案

#1


14  

What you want is a relative import like:

你想要的是一个相对导入,如:

from ..a.b import module

来自..a.b导入模块

The problem with this is that it doesn't work if you are calling test_file.py as your main module. As stated here:

这样做的问题是,如果您将test_file.py作为主模块调用它,它将不起作用。如上所述:

Note that both explicit and implicit relative imports are based on the name of the current module. Since the name of the main module is always "main", modules intended for use as the main module of a Python application should always use absolute imports.

请注意,显式和隐式相对导入都基于当前模块的名称。由于主模块的名称始终是“main”,因此用作Python应用程序主模块的模块应始终使用绝对导入。

So, if you want to call test_file.py as your main module, then you should consider changing the structure of your modules and using an absolute import, else just use the relative import from above.

因此,如果要将test_file.py作为主模块调用,则应考虑更改模块的结构并使用绝对导入,否则只需使用上面的相对导入。

#2


10  

  1. The directory a needs to be a package. Add an __init__.py file to make it a package, which is a step up from being a simple directory.

    目录a需要是一个包。添加一个__init__.py文件使其成为一个包,这是一个简单的目录。

  2. The directory b also needs to be a subpackage of a. Add an __init__.py file.

    目录b也需要是a的子包。添加__init__.py文件。

  3. The directory test should probably also be a package. Hard to say if this is necessary or not. It's usually a good idea for every directory of Python modules to be a formal package.

    目录测试应该也可能是一个包。很难说这是否有必要。通常,每个Python模块目录都是一个正式的包,这通常是一个好主意。

  4. In order to import, the package needs to be on sys.path; this is built from the PYTHONPATH environment variable. By default the installed site-packages and the current working directory are (effectively) the only two places where a package can be found.

    为了导入,包需要在sys.path上;这是从PYTHONPATH环境变量构建的。默认情况下,已安装的站点包和当前工作目录(有效)是可以找到包的唯一两个位置。

    That means that a must either be installed, or, your current working directory must also be a package one level above a.

    这意味着必须安装一个,或者,您当前的工作目录也必须是一个高于a的包。

    OR, you need to set your PYTHONPATH environment variable to include a.

    或者,您需要设置PYTHONPATH环境变量以包含a。

http://docs.python.org/tutorial/modules.html#the-module-search-path

http://docs.python.org/tutorial/modules.html#the-module-search-path

http://docs.python.org/using/cmdline.html#envvar-PYTHONPATH

http://docs.python.org/using/cmdline.html#envvar-PYTHONPATH

Also, http://docs.python.org/library/site.html for complete information on how sys.path is built.

另外,http://docs.python.org/library/site.html获取有关如何构建sys.path的完整信息。

#3


7  

The first thing to do would be to quickly browse the official docs on this.

首先要做的是快速浏览官方文档。

To make a directory a package, you'll have to add a __init__.py file. This means that you'll have such a file in the a and b directories. Then you can directly do an

要使目录成为包,您必须添加__init__.py文件。这意味着您将在a和b目录中拥有此类文件。然后你可以直接做一个

import a.b.module

But you'll have to refer to it as a.b.module which is tedious so you can use the as form of the import like so

但你必须将它称为a.b.module,这是乏味的,所以你可以使用像这样的导入形式

import a.b.module as mod #shorter name

and refer to it as mod.

并将其称为mod。

Then you can instantiate things inside mod using the regular conventions like mod.shape().

然后,您可以使用常规约定(如mod.shape())在mod中实例化事物。

There are a few other subtleties. Please go through the docs for details.

还有其他一些细微之处。请仔细阅读文档了解详情。

#1


14  

What you want is a relative import like:

你想要的是一个相对导入,如:

from ..a.b import module

来自..a.b导入模块

The problem with this is that it doesn't work if you are calling test_file.py as your main module. As stated here:

这样做的问题是,如果您将test_file.py作为主模块调用它,它将不起作用。如上所述:

Note that both explicit and implicit relative imports are based on the name of the current module. Since the name of the main module is always "main", modules intended for use as the main module of a Python application should always use absolute imports.

请注意,显式和隐式相对导入都基于当前模块的名称。由于主模块的名称始终是“main”,因此用作Python应用程序主模块的模块应始终使用绝对导入。

So, if you want to call test_file.py as your main module, then you should consider changing the structure of your modules and using an absolute import, else just use the relative import from above.

因此,如果要将test_file.py作为主模块调用,则应考虑更改模块的结构并使用绝对导入,否则只需使用上面的相对导入。

#2


10  

  1. The directory a needs to be a package. Add an __init__.py file to make it a package, which is a step up from being a simple directory.

    目录a需要是一个包。添加一个__init__.py文件使其成为一个包,这是一个简单的目录。

  2. The directory b also needs to be a subpackage of a. Add an __init__.py file.

    目录b也需要是a的子包。添加__init__.py文件。

  3. The directory test should probably also be a package. Hard to say if this is necessary or not. It's usually a good idea for every directory of Python modules to be a formal package.

    目录测试应该也可能是一个包。很难说这是否有必要。通常,每个Python模块目录都是一个正式的包,这通常是一个好主意。

  4. In order to import, the package needs to be on sys.path; this is built from the PYTHONPATH environment variable. By default the installed site-packages and the current working directory are (effectively) the only two places where a package can be found.

    为了导入,包需要在sys.path上;这是从PYTHONPATH环境变量构建的。默认情况下,已安装的站点包和当前工作目录(有效)是可以找到包的唯一两个位置。

    That means that a must either be installed, or, your current working directory must also be a package one level above a.

    这意味着必须安装一个,或者,您当前的工作目录也必须是一个高于a的包。

    OR, you need to set your PYTHONPATH environment variable to include a.

    或者,您需要设置PYTHONPATH环境变量以包含a。

http://docs.python.org/tutorial/modules.html#the-module-search-path

http://docs.python.org/tutorial/modules.html#the-module-search-path

http://docs.python.org/using/cmdline.html#envvar-PYTHONPATH

http://docs.python.org/using/cmdline.html#envvar-PYTHONPATH

Also, http://docs.python.org/library/site.html for complete information on how sys.path is built.

另外,http://docs.python.org/library/site.html获取有关如何构建sys.path的完整信息。

#3


7  

The first thing to do would be to quickly browse the official docs on this.

首先要做的是快速浏览官方文档。

To make a directory a package, you'll have to add a __init__.py file. This means that you'll have such a file in the a and b directories. Then you can directly do an

要使目录成为包,您必须添加__init__.py文件。这意味着您将在a和b目录中拥有此类文件。然后你可以直接做一个

import a.b.module

But you'll have to refer to it as a.b.module which is tedious so you can use the as form of the import like so

但你必须将它称为a.b.module,这是乏味的,所以你可以使用像这样的导入形式

import a.b.module as mod #shorter name

and refer to it as mod.

并将其称为mod。

Then you can instantiate things inside mod using the regular conventions like mod.shape().

然后,您可以使用常规约定(如mod.shape())在mod中实例化事物。

There are a few other subtleties. Please go through the docs for details.

还有其他一些细微之处。请仔细阅读文档了解详情。