0

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
4
  • 1
    Use:os.getcwd() This will show you what is the current directory you are running this code. and use os.chdir('/home/user1/python_test/') To change it Commented Oct 7, 2020 at 10:06
  • It is the same result: os.getcwd() outputs /home/user1/python_test/ Commented Oct 7, 2020 at 10:14
  • 1
    The example works perfectly fine on my machine. There must be something else with your Python environment Commented Oct 7, 2020 at 10:20
  • uhm, and what could I search to know the problem? It is strange for me too, usually it works without error. Commented Oct 7, 2020 at 10:27

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

oh, sorry, I didn't mention it, but I also have that file inside Aux/
0

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.

Comments

0

python or python3 was not working for me. I have solved the problem by putting this line sys.path.insert(0, os.getcwd()) after importing os.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.