modules in different folders but under same namespace
Hi, I have a package structured like this: package/__init__.py src/ __init__.py mod1.py share/__init__.py mod2.py Now I can import them as: package.src.mod1 package.share.mod2 How can I use Distutils to be able to import both of them under the same "package" namespace as: import package.mod1 import package.mod2 ? Thanks a lot in advance for your kind help! Best regards, Ric.
On Thu, Feb 4, 2010 at 5:53 PM, Riccardo-Maria BIANCHI <riccardo.maria.bianchi@cern.ch> wrote:
Hi,
Hi,
I have a package structured like this:
package/__init__.py src/ __init__.py mod1.py share/__init__.py mod2.py
Now I can import them as: package.src.mod1 package.share.mod2
How can I use Distutils to be able to import both of them under the same "package" namespace as:
import package.mod1 import package.mod2
unrelated to Distutils but, What you can do is add these line in package/__init__.py : from package.src import mod1 from package.share import mod2 Beware though, that this means the modules will be loaded even if "import package" is called, which could have some unwanted side effects depending on the code. Regards Tarek -- Tarek Ziadé | http://ziade.org
On 2010-02-04 11:05 AM, Tarek Ziadé wrote:
On Thu, Feb 4, 2010 at 5:53 PM, Riccardo-Maria BIANCHI <riccardo.maria.bianchi@cern.ch> wrote:
Hi,
Hi,
I have a package structured like this:
package/__init__.py src/ __init__.py mod1.py share/__init__.py mod2.py
Now I can import them as: package.src.mod1 package.share.mod2
How can I use Distutils to be able to import both of them under the same "package" namespace as:
import package.mod1 import package.mod2
unrelated to Distutils but,
What you can do is add these line in package/__init__.py :
from package.src import mod1 from package.share import mod2
Beware though, that this means the modules will be loaded even if "import package" is called, which could have some unwanted side effects depending on the code.
This does not work. You cannot "import package.mod1" under this scenario. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
On Thu, Feb 4, 2010 at 6:11 PM, Robert Kern <robert.kern@gmail.com> wrote:
On 2010-02-04 11:05 AM, Tarek Ziadé wrote:
On Thu, Feb 4, 2010 at 5:53 PM, Riccardo-Maria BIANCHI <riccardo.maria.bianchi@cern.ch> wrote:
Hi,
Hi,
I have a package structured like this:
package/__init__.py src/ __init__.py mod1.py share/__init__.py mod2.py
Now I can import them as: package.src.mod1 package.share.mod2
How can I use Distutils to be able to import both of them under the same "package" namespace as:
import package.mod1 import package.mod2
unrelated to Distutils but,
What you can do is add these line in package/__init__.py :
from package.src import mod1 from package.share import mod2
Beware though, that this means the modules will be loaded even if "import package" is called, which could have some unwanted side effects depending on the code.
This does not work. You cannot "import package.mod1" under this scenario.
Right, only "from package import mod1" works. Not sure why you want to remove the __init__.py files in sub and share in your solution though. -- Tarek Ziadé | http://ziade.org
On 2010-02-04 11:24 AM, Tarek Ziadé wrote:
Not sure why you want to remove the __init__.py files in sub and share in your solution though.
It prevents accidentally importing both package.src.mod1 and package.mod1 and helps readers of the code understand that only package.mod1 is correct. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
On 2010-02-04 10:53 AM, Riccardo-Maria BIANCHI wrote:
Hi,
I have a package structured like this:
package/__init__.py src/ __init__.py mod1.py share/__init__.py mod2.py
Now I can import them as: package.src.mod1 package.share.mod2
How can I use Distutils to be able to import both of them under the same "package" namespace as:
import package.mod1 import package.mod2
?
Thanks a lot in advance for your kind help!
Remove the package/src/__init__.py and package/share/__init__.py . In your package/__init__.py, append the src/ and share/ directories to the __path__ list. E.g. import os import pkgutil for subdir in ['src', 'share']: __path__.append(os.path.join(os.path.dirname(__file__), subdir)) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
On 2010-02-04 11:06 AM, Robert Kern wrote:
On 2010-02-04 10:53 AM, Riccardo-Maria BIANCHI wrote:
Hi,
I have a package structured like this:
package/__init__.py src/ __init__.py mod1.py share/__init__.py mod2.py
Now I can import them as: package.src.mod1 package.share.mod2
How can I use Distutils to be able to import both of them under the same "package" namespace as:
import package.mod1 import package.mod2
?
Thanks a lot in advance for your kind help!
Remove the package/src/__init__.py and package/share/__init__.py . In your package/__init__.py, append the src/ and share/ directories to the __path__ list.
See this essay for more documentation on this feature: http://www.python.org/doc/essays/packages.html -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
Thanks Robert and Tarek for your answer, I implemented the __path__ solution, and it works perfectly. Thanks! and sorry if I was off-topic: I thought that distutils had options for multi-folders setup, for "package_dir" and "packages" variables. Best regards, Ric. Robert Kern wrote:
On 2010-02-04 11:06 AM, Robert Kern wrote:
On 2010-02-04 10:53 AM, Riccardo-Maria BIANCHI wrote:
Hi,
I have a package structured like this:
package/__init__.py src/ __init__.py mod1.py share/__init__.py mod2.py
Now I can import them as: package.src.mod1 package.share.mod2
How can I use Distutils to be able to import both of them under the same "package" namespace as:
import package.mod1 import package.mod2
?
Thanks a lot in advance for your kind help!
Remove the package/src/__init__.py and package/share/__init__.py . In your package/__init__.py, append the src/ and share/ directories to the __path__ list.
See this essay for more documentation on this feature:
On Thu, Feb 04, 2010 at 05:53:16PM +0100, Riccardo-Maria BIANCHI wrote:
Now I can import them as: package.src.mod1 package.share.mod2
How can I use Distutils to be able to import both of them under the same "package" namespace as:
import package.mod1 import package.mod2
In package/__init__.py: import src.mod1 as mod1 import share.mod2 as mod2 And then you can use just one "import package" statement. If you want src and share to be separately installable then it gets more complicated (you'd need namespace packages etc), but why complicated when simple works? Regards Floris -- Debian GNU/Linux -- The Power of Freedom www.debian.org | www.gnu.org | www.kernel.org
On 2010-02-04 11:37 AM, Floris Bruynooghe wrote:
On Thu, Feb 04, 2010 at 05:53:16PM +0100, Riccardo-Maria BIANCHI wrote:
Now I can import them as: package.src.mod1 package.share.mod2
How can I use Distutils to be able to import both of them under the same "package" namespace as:
import package.mod1 import package.mod2
In package/__init__.py:
import src.mod1 as mod1 import share.mod2 as mod2
And then you can use just one "import package" statement. If you want src and share to be separately installable then it gets more complicated (you'd need namespace packages etc), but why complicated when simple works?
Because simple doesn't work. You cannot do "import package.mod1" or "from package.mod1 import foo" with that scheme. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
At 05:53 PM 2/4/2010 +0100, Riccardo-Maria BIANCHI wrote:
Hi,
I have a package structured like this:
package/__init__.py src/ __init__.py mod1.py share/__init__.py mod2.py
Now I can import them as: package.src.mod1 package.share.mod2
How can I use Distutils to be able to import both of them under the same "package" namespace as:
import package.mod1 import package.mod2
Move mod1.py and mod2.py under package/, and delete the subdirectories. If you need backward compatibility, leave the old subdirectories and files in place, but have them import the new ones instead of containing any code. Then delete them later. Munging __path__ or using the package_dir options to setup() are both bad ideas in this case, unless there are other requirements you haven't stated. If you use the __path__ approach, you're going to make it more difficult for the distutils to figure out what to include in your source and binary distributions and installs, and if you use package_dir, you're going to end up with a different installation layout than your source layout. Both will likely lead to hassles down the road, even if they appear to work in the short term.
participants (5)
-
Floris Bruynooghe
-
P.J. Eby
-
Riccardo-Maria BIANCHI
-
Robert Kern
-
Tarek Ziadé