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 do

from modA.fileAA import <something>

      

I am getting the following error:

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

      

Note that the files __init__.py

are empty and use Python 3.

What am I missing or doing wrong here?

+5


source to share


4 answers


This is almost the question PYTHONPATH

from which you are using the script. 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 take a look sys.path

to check your way.

0


source


Since you wrote your code in fileBB.py

and are trying to import variables / functions / classes , etc. defined in fileAA.py

, you really need to do something like this:

  • First create an empty one __init__.py

    inside Boo .

  • Then try importing like this:

    from ..modA.fileAA import <something>
    
          



According to my experience with writing packages, it should work fine.

Note: Please comment if this doesn't work I will help but it shouldn't.

0


source


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

      

Have __init__.py

in root and then use import as

from main_package.modA.fileAA import something

      

Run with a file driver

inside main_package

then run it will work.

0


source


I found a solution on Win7, a little workaround, really:

I figured out the tutorial for creating custom modules provided by the PythonGuru.com website when I also got

ModuleNotFoundError: no module named 'mymodule'

...

System Properties >> Environment Variables >> User and System Variables are fully highlighted, so I don't think so.

That being said, I added a text file called mymodule.py, a custom module to import, into the ... Python \ Python37 \ DLLs folder for it to work.

For some reason the console (which is the Windows command line that I use to run these programs, right? (Very new to Python and the associated jargon)) won't look for mymodule.py in My Docs where I have all the .py files I created for going through these tutorials. The file I was trying to run as a program contained the code

import mymodule

print(mymodule.foo)


print(mymodule.hello())

and the custom module itself contained

foo = 100

def Hello():
....print("Hiya, this is from mymodule!")

      

Also tried import mymodule.py

and also typing the code directly into the console instead of running the file. Nothing worked until I added mymodule.py to the DLL folder.

I have read elsewhere that under certain circumstances Python will not look for a tree for different things, but have not seen how it has been applied, as well as on another error, if I recall correctly.

How can I avoid this error without adding a custom module to my DLL folder?

Thanks for the help,
J

-1


source







All Articles