Python's import situation has driven me to the brink of imsanity

dimva13 at gmail.com dimva13 at gmail.com
Sat Feb 6 21:47:54 EST 2016


No, that's not a typo, it's the name of a package I created. :)

The problems I have with python's import system are detailed in the README of my package here: https://github.com/vadimg/imsanity

Basically, relative imports are broken if you like running scripts as executables, so the only real solution is modifying PYTHONPATH every time you switch projects, which is also not ideal.

Some people suggest adding various boilerplate to the beginning of every file to fix this, ranging from the crazy stuff in PEP 0366 to the slightly more sane sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), path_to_root))), where path_to_root is some number of '../'s depending on the file's location. Neither can be used indiscriminately as a macro, which is annoying.

Imsanity allows you to make imports usable (not ideal, but at least usable) for python projects without having to manage PYTHONPATHs or do whacky stuff like running files with python -m or put even whackier boilerplate at the top of every file. And all it requires is 'import imsanity' at the top of every file. You can put it in a macro or even just type it because it's short and easy to remember.

My question is: is this crazy? Please tell me there's a better way and I just wasted my time creating this package. There's nothing I'd like to hear more.


More information about the Python-list mailing list