简体   繁体   English

ImportError:如何导入 python 模块?

[英]ImportError: How do I import a python module?

I've tried importing a Python module and failed.我尝试导入 Python 模块但失败了。 Here's my folder hierarchy:这是我的文件夹层次结构:

package/
  folder/
    a.py
  utils/
    b.py

In module a.py I've tried importing b.py but received an ImportError .在模块a.py ,我尝试导入b.py但收到ImportError

How do I use function test from module b to a.py ?如何使用 function test从模块ba.py

Files:文件:

a.py一个.py

def usetest():
    test()

b.py b.py

def test():
    print("hello world")

Importing of Python modules can be done with a few different syntaxes:可以使用几种不同的语法导入 Python 模块:

  • A simple import... statement: import package.utils.b will now let you use package.utils.b.test() .一个简单的import...语句: import package.utils.b现在将允许您使用package.utils.b.test() Unfortunately it's quite long.不幸的是它很长。
  • An import... as... statement: import package.utils.b as b will let you use b.test() . import... as...语句: import package.utils.b as b会让您使用b.test()
  • A from... import... statement: from package.utils.b import test will let you use test() . from... import...语句: from package.utils.b import test将让您使用test()
  • A from... import... as... statement: from package.utils.b import test as test_me will let you use test_me() . from... import... as...语句: from package.utils.b import test as test_me将让您使用test_me()

All of these options will run the exact same function. Try putting them at the top of a.py .所有这些选项都将运行完全相同的 function。尝试将它们放在a.py的顶部。

Specifying the whole path, package.utils.b is called absolute form.指定整个路径, package.utils.b称为绝对形式。 You can also import in relative form:您还可以以相对形式导入:

  • import..utils.b as b will let you use b.test() . import..utils.b as b会让你使用b.test() Notice the 2 dots at the start, saying go up one folder .注意开头的 2 个点,表示go up one folder
  • from..utils.b import test will let you use test() . from..utils.b import test会让你使用test()
  • from..utils.b import test as test_me will let you use test_me() . from..utils.b import test as test_me会让你使用test_me()

Each dot at the start specifies go up one folder, except one, which says "this folder".开头的每个点指定 go 向上一个文件夹,除了一个,上面写着“这个文件夹”。

If the main file you try to run is inside the package ( a.py ), you should switch to the directory that contains package, and run the file using -m .如果您尝试运行的主文件在 package ( a.py ) 中,您应该切换到包含 package 的目录,并使用-m运行该文件。 In your case, switch to the directory that contains package , and run python -m package.folder.a .在您的情况下,切换到包含package的目录,然后运行python -m package.folder.a

For more information about importing modules, see the Python Docs .有关导入模块的更多信息,请参阅Python 文档

You can also dynamically import Python modules using their full path.您还可以使用完整路径动态导入 Python 模块。 It's more advanced and won't be covered in this answer.它更高级,不会在此答案中涵盖。

package/ 
  folder/
    a.py
  utils/
    b.py

import:进口:

if package is your main folder then use below syntax to import modules.如果 package 是您的主文件夹,则使用以下语法导入模块。

import utils.b as b
b.test() 
**or** 
from utils import b
**or**
To import only method
from utils.b import test
**or**
with alias name
from utils.b import test as my_test (my_test is the method alias name)

Import:进口:

import folder.b as b b.test() or from folder.b import test or with alias name from folder.b import test as my_test (my_test is the alias name) import folder.b as b b.test() or from folder.b import test or with alias name from folder.b import test as my_test (my_test 是别名)

If the main file you try to run is inside the package (a.py), you should switch to the directory that contains package, and run the file using -m如果您尝试运行的主文件在 package (a.py) 中,您应该切换到包含 package 的目录,并使用-m运行该文件

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

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