0

I like approaches of local package management in Flutter/Dart and Rust very well, and hope there could exist some similar solution in Python.

For example, I have a utility python package at /path/of/the/utility containing dozens of python files, and a myapp package at /path/to/my/app. For myapp to depend on utility, if I were using Flutter/Dart or using Rust, I could write down a line like:

utility:
  path: /path/of/the/utility

And then happily import it like import 'package:utility/somefile.dart. (The Rust case is similar so I do not repeat.)

However, with Python, I have not found out such solution (pip does not seem to know this; neither is conda). Currently I have to use the following ugly approach:

// myapp/somefile.py
import sys
sys.path.append('/path/of/the/utility')
import utility

and also manipulate my IDE (Intellij IDEA in my case) such that it understands this myapp package depends on utility package and do not give me a big red underline.

Remark: It may not be possible (is it?) to let /path to be the package root and myapp to be a subpackage, because: (1) The myapp, for example, can be a Django project. (2) It will be hard to configure the ide (Intellij IDEA in my case) if the huge project is one Python package since it is mixed with other languages.

Question: Can I do something like the Flutter/Dart or Rust approach? Thanks!

ch271828n
  • 15,854
  • 5
  • 53
  • 88
  • [Import a file from a subdirectory?](https://stackoverflow.com/questions/1260792/import-a-file-from-a-subdirectory) – Jab Mar 03 '22 at 13:47
  • Not sure I understand. Normally in Python I would install that utility: `python -m pip install path/to/utility`. Or are we missing some info here? Is this utility not installable? – sinoroc Mar 03 '22 at 13:50
  • @Jab Sorry but no. They are, for example, `/.../myrepository/backend/abcde/fghij/utility` and `/.../myrepository/fronend/something/myapp`, and I do not want `/.../myrepository` to be the root of package - that would make my *whole* monorepo a mess... – ch271828n Mar 03 '22 at 13:51
  • @sinoroc the utility is just a bunch of `.py` that I have written. No `setup.py` etc. Is it ok? – ch271828n Mar 03 '22 at 13:52
  • Having a correct packaging (with `setup.py` or `pyproject.toml` or whatever) would make things much easier. -- Then most of the common tools have issue with mono-repos, mono-repos need specific tooling (maybe look up `bazel`). -- Otherwise, I hate to say it, but maybe add `path/to/utility` to the `PYTHONPATH` environment variable. This and `sys.path` modifications are ugly. -- There are many ways to do what you want, all with their pros and cons. But correct packaging is probably the best. – sinoroc Mar 03 '22 at 14:06
  • @sinoroc Thanks! I will consider using `setup.py` and install it using `pip install utility` and import it as if it is 3rd party library. – ch271828n Mar 03 '22 at 23:58

0 Answers0