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

Viewed 731

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 code :


#!/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:

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

Not sure if it is an Ubuntu thing.

Please help

When running ls -la in the folder where both files exist:

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?

3 Answers

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:

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+

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

import imp

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

you can create a package from view.py. see here: link

then you can import that package from anywhere in the system

Relative imports only works within a package. When you run a python script within a directory that was supposed to be a package, this directory stops being a 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.

Related