Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 -- Module not found

I have the following file structure...

 > Boo
    > ---modA
    > ------__init__.py
    > ------fileAA.py
    > ---modB
    > ------__init__.py
    > ------fileBB.py

When inside fileBB.py I am doing

from modA.fileAA import <something>

I get the following error:

from modA.fileAA import <something>
ModuleNotFoundError: No module named 'modA'

Note that the __init__.py files are empty and using Python 3.

What am I missing or doing wrong here?

like image 275
DimSarak Avatar asked Jul 27 '17 15:07

DimSarak


Video Answer


2 Answers

This is almost certainly a PYTHONPATH issue of where you're running your script from. In general this works:

$ ls modA/
fileAA.py  __init__.py
$ cat modA/fileAA.py 
x = 1
$ python3
Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170118] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from modA.fileAA import x
>>> x
1

You can look at sys.path to inspect your path.

like image 58
keredson Avatar answered Sep 29 '22 14:09

keredson


main_package
├── __init__.py
├── modA
│   ├── fileAA.py
│   └── __init__.py
└── modB
    ├── fileBB.py
    └── __init__.py

Have an __init__.py in the root directory and then use import like

from main_package.modA.fileAA import something

Run using a driver file inside main_package then run, it'll work.

like image 44
Vishnudev Avatar answered Sep 29 '22 16:09

Vishnudev