Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python packages/modules in different subdirs

Tags:

python

I'm relatively new to Python, and I'm struggling to understand if what I'm trying to do is just not possible, or if I am just doing it wrong. I admit that I come from Perl - where the rules for where you put your packages, and how the directories should look like is a bit looser, so my point of view my be not so pythonic.

Also, this is a different version of a question I've asked a couple of times here and here and I keep getting answers (that I am grateful for) that say I should not, but never "it's impossible" or "here's how you do it, though you should not". If something is possible, but not advisable, I would rather be able to find out why by trying and seeing and if it is impossible by design, I'd like to know (and stop banging my head against a wall).

Apologies in advance for the long intro - here's the question, finally.

I have this nice module - svgpathtools - that contains methods, classes and sits in an install-specific directory (inside the Python install that comes with Blender), directory that looks like this:

/home/simone/blender/blender-2.92.0-linux64/2.92/python/lib/python3.7/site-packages/svgpathtools
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-37.pyc
│   ├── bezier.cpython-37.pyc
│   ├── document.cpython-37.pyc
│   ├── misctools.cpython-37.pyc
│   ├── parser.cpython-37.pyc
│   ├── path.cpython-37.pyc
│   ├── paths2svg.cpython-37.pyc
│   ├── polytools.cpython-37.pyc
│   ├── smoothing.cpython-37.pyc
│   ├── svg_io_sax.cpython-37.pyc
│   └── svg_to_paths.cpython-37.pyc
├── bezier.py
├── document.py
├── misctools.py
├── parser.py
├── path.py
├── paths2svg.py
├── polytools.py
├── smoothing.py
├── svg_io_sax.py
└── svg_to_paths.py

1 directory, 22 files

I want to add functionality to it without messing with that directory and without subclassing. The first for obvious reasons, the second because for now I am only adding a couple of methods max, so it's not worth maintaining a whole subclass (for now at least). So my idea is to have a directory that looks like this:

/home/simone/blender_learning/mesh-20210920
└── svgpathtools
    ├── __init__.py
    └── distance.py

Now: if I keep svgpathtools/__init__.py, python (3.8.10) won't find the original svgpathtools in the Blender install subdirectory, so all its functionality is gone and if I remove svgpathtools/__init__.py, python won't find svgpathtools/distance.py so import svgpathtools.distance throws an error.

Which one is it then:

  • it's impossible: because of the way python looks for packages, you can't have sub-packages in a different place than the main package
  • I am doing it wrong in some way (please tell me why)

Pretty please: don't just tell me I should not do it without having told me which one it is. I know that doable <> advisable (playing with explosives? petting hungry leopards? eating fugu?)

like image 990
simone Avatar asked Nov 17 '25 04:11

simone


1 Answers

When you import a module import <module> python looks through the paths for the first match

import sys
print(sys.path)

So having a module with the same name ends up shadowing it, rather than what you want which is keep looking and merge them. So you'll have to name your extensions package something else to not conflict with it.

You can probably get the behaviour you want by rolling your own import method with importlib and a wrapper around the module to fallback to the other but it seems hardly worth it.

like image 51
Steven Summers Avatar answered Nov 19 '25 19:11

Steven Summers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!