【问题标题】:How to import python functions from different file (in Ubuntu)如何从不同的文件中导入 python 函数(在 Ubuntu 中)
【发布时间】:2020-05-13 06:58:23
【问题描述】:

我有一个名为hero.py 的python 文件,它引用位于views.py 中的其他python 文件(这两个文件都存在于同一个文件夹中)。

hero.py代码:


#!/usr/bin/env python3

from .views import main, returnSum, most_frequent, find_mine_site_view_id, get_user_Activity, initialise_analytics_reporting

list_of_mines = ['mine1', 'mine2', 'mine3']

start_date = 'yesterday'
end_date = 'yesterday'

main(list_of_mines, start_date, end_date)

使用chmod +x hero.py 使文件可执行并在hero.py 顶部添加#!/usr/bin/env python3 后,运行./hero.py 时出现此错误:

Traceback (most recent call last):
  File "./hero.py", line 2, in <module>
    from .views import main, returnSum, most_frequent, find_mine_site_view_id, get_user_Activity, initialise_analytics_reporting
ModuleNotFoundError: No module named '__main__.views'; '__main__' is not a package

我知道我的views.py不是一个包,我只是想导入views.py中存在的函数

不确定它是否是 Ubuntu 的东西。

请帮忙

在两个文件都存在的文件夹中运行ls -la 时:

total 72
drwxrwxr-x 8 llewellyn llewellyn  4096 May 13 06:39 .
drwxrwxr-x 6 llewellyn llewellyn  4096 May 11 19:19 ..
drwxrwxr-x 3 llewellyn llewellyn  4096 May  7 08:52 .idea
-rw-rw-r-- 1 llewellyn llewellyn     0 May  7 07:21 __init__.py
drwxrwxr-x 2 llewellyn llewellyn  4096 May 13 06:18 __pycache__
-rwxrwxr-x 1 llewellyn llewellyn    86 May 12 17:39 admin.py
-rwxrwxr-x 1 llewellyn llewellyn   108 May 12 17:40 apps.py
drwxrwxr-x 3 llewellyn llewellyn  4096 May  7 09:04 config
drwxrwxr-x 3 llewellyn llewellyn  4096 May  9 11:34 migrations
-rwxrwxr-x 1 llewellyn llewellyn  2607 May 12 17:40 models.py
-rwxrwxr-x 1 llewellyn llewellyn 16146 May 13 06:17 views.py

我做错了什么?

【问题讨论】:

    标签: python django python-3.x ubuntu


    【解决方案1】:

    只需删除视图前的点:

    from views import main, returnSum, most_frequent, ...
    #    ^ here
    

    编辑:
    从子文件夹导入:
    使用. 作为分隔符:
    当文件位于这样的位置时:

    someFolder
    +-main.py <- file with import
    `-the
       `-path
         `-to
           `-module.py <- in this file is func1
    

    做:

    from the.path.to.module import func1
    # imports func1 from file module.py
    # then use like:
    func1()
    

    from the.path.to import module
    # imports whole module
    # then use like:
    module.func1()
    

    import the.path.to.module
    # use:
    the.path.to.module.func1()
    

    import the.path.to.module as mod
    #imports the.path.to.module that is accessed by identifier mod
    #so use it like
    mod.func1()
    

    您也可以组合asfrom

    from the.path.to import module as mod
    #use:
    mod.func1()
    

    当路径为字符串或文件不是子文件夹时,可以这样做:
    对于 Python 3.5+

    import importlib.util
    spec = importlib.util.spec_from_file_location("module.name", "/the/path/to/module.py")
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    # then use like this:
    module.func1()
    

    对于 Python 2

    import imp
    
    module = imp.load_source('module.name', '/the/path/to/module.py')
    module.func1()
    

    【讨论】:

    • 如果我想从位于特定路径的文件中导入某些内容。我该怎么做?
    【解决方案2】:

    您可以从 view.py 创建一个包。看这里:link

    然后您可以从系统中的任何位置导入该包

    【讨论】:

      【解决方案3】:

      相对导入仅适用于包内。当您在应该是包的目录中运行 python 脚本时,该目录将不再是包。

      如果您不想创建包,只需将views.py 放在module search paths 之一中,然后使用绝对导入。

      【讨论】:

        猜你喜欢
        • 2021-11-12
        • 2014-06-08
        • 2020-08-22
        • 2011-11-22
        • 1970-01-01
        • 1970-01-01
        • 2016-02-19
        • 2016-08-05
        • 2019-12-30
        相关资源
        最近更新 更多