Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import module without class in Python?

I have 4 files in my project:

project/__init__.py
project/app.py
project/mod_x.py
project/mod_y.py

In mod_x.py I have a class (e.g. ModX) In mod_y.py I have just one function.

I import modules from app.py as follows:

from .mod_x import ModX
import .mod_y

I get an error:

ImportError: No module named 'mod_y'

Before I created init.py I didn't have that kind of problems (of course, I dont put "." before module name).

How to import module which doesn't have the class inside in Python3 with init.py file inside the current directory?

like image 473
trojek Avatar asked Oct 29 '25 05:10

trojek


1 Answers

Relative imports are only available for from...import syntax.

You could import that function this way:

from .mod_y import FUNCTION_NAME

Module could be imported this way:

from . import mod_y
like image 68
bakatrouble Avatar answered Oct 30 '25 22:10

bakatrouble