Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python modules, referencing other modules within the same package?

I've got a simple python package with the following directory structure:

wibble
|
|-----foo
|      |----ping.py
|
|-----bar
|      |----pong.py

Simple question: How would I reference a function / class in ping.py from pong.py? Looking through the documentation, this appears to be as simple as creating __init__.py files in the root of wibble, foo and bar and then from pong.py doing something like from wibble.foo.ping import important_function. However, trying this leads to No module named wibble.foo.ping.

I'm sure I've missed something in the documentation somewhere, because this must be possible to do. I'm also a little hazy on the differentiation between a module and a namespace, my background is c#.net, so any analogies here will be useful.

like image 782
growse Avatar asked Jun 18 '26 01:06

growse


1 Answers

you need to add wibble to python path (see the official documentation), one way is

import sys
sys.path.append('/path/to/wibble')

Another way is with the environment variable PYTHONPATH

export PYTHONPATH=$PYTHONPATH:/path/to/wibble
like image 72
diegueus9 Avatar answered Jun 19 '26 16:06

diegueus9