looo wrote:Isn't that exactly what we are trying to do? It's all about removing the mod-directories from the sys.path to get rid of the naming issues. But as we can't do this right now, we have to slowly get to this goal. The first step is to give new modules the possibility to use proper imports. (PR)
Yeah, this is what I meant with my question. If I understand it correctly right now the modules are first added to the sys.path
and then a new function is added to let a module remove itself from sys.path
again if it wants to. Is that correct?
Why isn't it possible to simply move a FreeCAD module (or all FreeCAD modules for that matter) to freecad.modules
, iterate over the modules in this path using for package in pkgutil.walk_packages(freecad.modules.__path__)
and for each of them run their Init.py
This ties in to the point about __path__
I do not really understand what a namespace package is, but using FreeCAD.__path__ gives us the possibility to have everything available under the FreeCAD name-space. And this is quite simple.
and install it together with it's modules to a normal location.
can you explain this? What is meant by a normal location?
And then just do a pkgutil.iter_modules("FreeCAD.Modules") to iterate over the existing modules and init them?
This should be equivalent to adding the locations of FreeCAD modules to FreeCAD.__path__. I don't see why this should be simpler.
The PyMOTW explains it best imho https://pymotw.com/2/pkgutil/
, sample code lives here if you want to try it out https://bitbucket.org/dhellmann/pymotw/ ... ?at=master
Basically a namespace package is the normal Pythonic way to have a package span multiple directories on the actual filesystem.
By adding this:
Code: Select all
__path__ = pkgutil.extend_path(__path__, __name__)
to either freecad
it's possible to let regular Python modules irrespective of where they are stored on the actual filesystem add functionality (ie FreeCAD modules in this case) to the freecad
package. This means a FreeCAD module could be installed using pip and just live in site-packages like any normal Python package.
It also means if you want to try out some module locally you can just download it, set the PYTHONPATH=/dir/to/local/freecad/module
env var and as long as it's got a freecad.modules.<something>
package it'll be picked up.
Note that Python 3.3+ will automatically create namespace packages
when no __init__.py
is present making this even easier.
I hope I managed to explain my questions. It seems to me there are standard Pythonic ways to solve these issues which can easily be leveraged.
I'm probably missing some context though, so if I missed something please correct me