Importing package as a submodule
Posted
by
wecac
on Stack Overflow
See other posts from Stack Overflow
or by wecac
Published on 2010-12-25T15:41:40Z
Indexed on
2010/12/25
15:54 UTC
Read the original article
Hit count: 198
Hi,
I have a package 3rd party open source package "foo"; that is in beta phase and I want to tweak it to my requirements. So I don't want to get it installed in /usr/local/lib/python or anywhere in current sys.path as I can't make frequent changes in top level packages.
foo/
__init__.py
fmod1.py
import foo.mod2
fmod2.py
pass
I want to install the the package "foo" as a sub package of my namespace say "team.my_pkg". So that the "fullname" of the package becomes "team.my_pkg.foo" without changing the code in inner modules that refer "team.my_pkg.foo" as "foo".
team/
__init__.py
my_pkg/
__init__.py
foo/
fmod1.py
import foo.mod2
fmod2.py
pass
One way to do this is to do this in team.my_pkg.init.py:
import os.path
import sys
sys.path.append(os.path.dirname(__file__))
But I think it is very unsafe. I hope there is some way that only fmod1.py and fmod2.py can call "foo" by its short name everything else should use its complete name "team.my_pkg.foo"
I mean this should fail outside team/my_pkg/foo:
import team.my_pkg
import foo
But this should succeed outside team/my_pkg/foo:
import team.my_pkg.foo
© Stack Overflow or respective owner