0

I'm getting a module import error.

My main file is /home/mininet/pythonscripts/import.py: Main file program

and my module file is /home/mininet/test/hello.py: module file program

The error I'm getting is:

File "import.py", line 7, in <module> from test.hello import sqr,print_func 
ImportError: No module named hello

i also added the __init__.py file in the module search path..please help!!

1 Answer 1

1

In order to import /home/mininet/test/hello.py as test.hello, you have to fulfill two requirements:

  1. /home/mininet/test/__init__.py must exist to mark test as a package.
  2. /home/mininet must be on sys.path so that Python finds test/hello.py when looking for test.hello.

Note that having /home/mininet/test on sys.path lets you import hello, but not import test.hello.

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

2 Comments

Thanks it works.But do we have to do it everytime i.e. add sys.path.insert() code in all our module dependent codes(if the module resides in another directory)..it's quite cumbersome.any other option
The script's path is always on sys.path, as is the standard library. So, one option is to have your script in the same directory as the package (e.g. in /home/mininet, or move test to /home/mininet/pythonscripts). Alternatively, you can use .pth files to add certain directories to sys.path automatically on startup.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.