Hello,
On Wed, 5 Aug 2015 18:23:20 +0300
Paul Sokolovsky
I'm sorry for possibly hijacking this thread, but it touches very much issue I had on my mind for a while: being able to run modules inside package as normal scripts. As this thread already has few people knowledgeable of peculiarities of package imports, perhaps they can suggest something.
Scenario:
There's a directory ("pkg" (representing Python namespace package)), and inside it, there's bar.py of not relevant content and foo.py with "from . import bar". What I'd like to do is (while inside "pkg" directory):
python3 foo.py
Current behavior:
SystemError: Parent module '' not loaded, cannot perform relative import
Expected behavior:
"from . import bar" in foo.py is successful.
Perhaps I was asking something dumb, or everyone just takes for granted that nowadays Python code can't be developed comfortably without IDE, or 2+ console windows open, or something. But I find it quite a sign of problem, because if one accepts that one can't just run a script right away, but needs to do extra steps, then well, that enterprisey niche is pretty crowded and there're more choices how to make it more complicated. So, I did my homework (beyond just googling, which unfortunately didn't turn up much), and being able to do it with a simple "loader" and "single command line switch" like: python3 -mruninpkg script.py arg1 arg2 arg3 restored my piece of mind. The script is here: https://github.com/pfalcon/py-runinpkg . Hope someone else will find its existence insightful, or maybe someone will suggest how to make it better (I see the bottleneck in that it's not possible to make mod.__name__ an empty string, and that's what would be needed here to avoid double-import problem). I actually did another googling session, and there's definitely a niche for such solution, like this 10-years old article shows: http://code.activestate.com/recipes/307772-executing-modules-inside-packages... If only there were good, widely known inventory of them for different usecases... -- Best regards, Paul mailto:pmiscml@gmail.com