Z.Grey
Z.Grey

Reputation: 164

Import subfolders gives error no module named

Iam using python 3.7. I try to import its2D_1.py file. While I am importing the subfolders it gives an error 'No module names'.

The subfolder has an init.py and I am at the right directory

In [26]: from __future__ import absolute_import
In [27]: from .examples.linear_elasticity.its2D_1 import *
ModuleNotFoundError     
ModuleNotFoundError: No module named 'examples.linear_elasticity'

The directory is script and the order of the folders script>examples>linear_elasticity>its2D_1.py

How could I import the linear_elasticity subfolder, thanks?

Upvotes: 0

Views: 1560

Answers (1)

Rahul
Rahul

Reputation: 577

If you are using python2 you need init.py. From python 3.4 it has been deprecated, you don't need an init.py file to specify if it's a package or not.

parent/
    __init__.py
    one/
        __init__.py
    two/
        __init__.py
    three/
        __init__.py

Importing parent.one will implicitly execute parent/init.py and parent/one/init.py. https://docs.python.org/3/reference/import.html From this link: foo.bar.baz. In this case, Python first tries to import foo, then foo.bar, and finally foo.bar.baz. If any of the intermediate imports fail, a ModuleNotFoundError is raised.

Upvotes: 1

Related Questions