简体   繁体   English

如何从不同的文件导入 python 函数(在 Ubuntu 中)

[英]How to import python functions from different file (in Ubuntu)

I have a python file called hero.py that refers to other python files located in views.py (Both these files exist in the same folder).我有一个名为hero.py的 python 文件,它引用位于views.py中的其他 python 文件(这两个文件都存在于同一个文件夹中)。

hero.py code: 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)

After making the file executable with chmod +x hero.py and adding #!/usr/bin/env python3 at the top of hero.py , I get this error when running ./hero.py :使用chmod +x hero.py使文件可执行并在 hero.py 顶部添加#!/usr/bin/env python3后,运行hero.py./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

I am aware that my views.py is not a package, I simply want to import the functions that exist within views.py我知道我的views.py 不是package,我只是想导入views.py中存在的函数

Not sure if it is an Ubuntu thing.不确定它是否是 Ubuntu 的东西。

Please help请帮忙

When running ls -la in the folder where both files exist:在两个文件都存在的文件夹中运行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

What am I doing wrong?我究竟做错了什么?

Just remove the dot before views:只需删除视图前的点:

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

Edit:编辑:
To import from subfolder:从子文件夹导入:
Use .使用. as separator:作为分隔符:
When file is located like this:当文件位于这样的位置时:

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

do:做:

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

or或者

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

or或者

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

or或者

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

You can combine as and from too:您也可以组合asfrom

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

When the path is string or file isn't subfolder, you can do this:当路径是字符串或文件不是子文件夹时,您可以这样做:
For Python 3.5+对于 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()

For Python 2对于 Python 2

import imp

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

you can create a package from view.py.您可以从 view.py 创建一个 package。 see here: link看这里:链接

then you can import that package from anywhere in the system然后您可以从系统中的任何位置导入该 package

Relative imports only works within a package.相对导入仅适用于 package。 When you run a python script within a directory that was supposed to be a package, this directory stops being a package.当您在应该是 package 的目录中运行 python 脚本时,该目录将不再是 package。

If you do not want to create a package, simply put views.py in one of the module search paths , then use absolute import.如果您不想创建 package,只需将 views.py 放在模块搜索路径之一中,然后使用绝对导入。

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

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