简体   繁体   中英

Can't import module from local folder? "No module named"

I don't find the mistake in the code nor in the call when trying an import test.

My directory structure is the following: (from /home/user1/python_test/ )

main.py
Aux/lib1.py
Aux/__init__.py

main.py:

from Aux.lib1 import fun_A

if __name__ == "__main__":
    fun_A()
    print("all done")

Aux/lib1.py:

def fun_A():
    print("A function called")

I'm executing from terminal ( python main.py from directory where main.py is), maybe is there a need to set pythonpath? I don't recall the need before, I've made some python programs like these some time ago (2/3 years)

I have also tried from .Aux.lib1 import fun_A instead of from Aux.lib1 import fun_A but nothing works. The error is:

  File "main.py", line 1, in <module>
    from Aux.lib1 import fun_A
ImportError: No module named Aux.lib1

Create a blank file with name __init__.py under the folder Aux. Without this file a directory can not be a module.

Oh my, It was a super silly mistake. I'm so used to execute inside a virtual environment that I was using python2 by mistake using python main.py . Executing python3 main.py worked for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM