0

I am facing an issue that is related to import path. I have a library file (func_a.py) as follows. This file is called from different directories. In such a case, how do I specify import path in client.py?

.
├── main.py
└── package_a
    ├── __init__.py
    ├── client.py
    └── func_a.py

The codes are as follows:

$ cat package_a/func_a.py                                                                                                                                                                                                                        
def something():
    print('something')

$ cat package_a/client.py                                                                                                                                                                                                                       
import func_a

func_a.something()

$ cat main.py                                                                                                                                                                                                                                    import package_a.func_a as func_a
import package_a.client as client

func_a.something()

This is the error. When I call client.py, the file misses func_a.py since the current directory is the root, not package_a/.

$ python main.py                                                                                                                                                                                                                                     Traceback (most recent call last):
  File "main.py", line 2, in <module>
    import package_a.client as client
  File "/home/jef/work/test/package_a/client.py", line 1, in <module>
    import func_a
ModuleNotFoundError: No module named 'func_a'

My python is 3.6. Thank you for your help.

Update

Although calling main.py is OK, calling client.py failed. I make both work.

$ cat client.py
from package_a import func_a

func_a.something()

$ python client.py        
Traceback (most recent call last):
  File "client.py", line 2, in <module>
    from package_a import func_a
ModuleNotFoundError: No module named 'package_a'
jef
  • 3,890
  • 10
  • 42
  • 76

1 Answers1

0
$ cat package_a/client.py
import func_a

^ This import statement is incorrect. To import the func_a module from the package_a package, use this import statement instead:

from package_a import func_a

You will need to ensure that the directory containing package_a is visible in sys.path.

wim
  • 338,267
  • 99
  • 616
  • 750
  • Thank you. I updated my post. I also want to run both main.py and client.py. – jef Apr 19 '18 at 18:15
  • 1
    As I mentioned, you will need to ensure that the directory containing `package_a` is visible in `sys.path`, and your new error suggests you didn't do that. As for `client.py`, running submodules of a package as scripts is considered an anti-pattern. Consider `python -m package_a.client` instead. – wim Apr 19 '18 at 18:18
  • Great. It makes sense. Using m option will be nice. – jef Apr 19 '18 at 18:20
  • I leave a [useful link](https://stackoverflow.com/questions/22241420/execution-of-python-code-with-m-option-or-not) for me since I am not familiar with m option. – jef Apr 19 '18 at 18:23