3 ms·
It has some wildly frustratingly unintuitive behaviours in precisely the wrong place for beginners: in between having everything in a single script and building
by rcxdude 5y ago
It has some wildly frustratingly unintuitive behaviours in precisely the wrong place for beginners: in between having everything in a single script and building a proper package, especially when you are invoking your script with 'python script.py' as opposed to say 'python -m scripts.script'.
- markmark 5y agoYeah. Start writing a program in 'myprogram.py' as things grow do the right thing and split a function out to its own file and import it. It doesn't work. Suddenly you need to learn a whole bunch about python modules and the import system and scripts vs modules, and some of the questions you have just literally have no good answer.
- mixmastamyk 5y agoImport by module: ⏵ ls module.py script.py ⏵ cat module.py def foo2(): print('foo2 called.') ⏵ cat script.py import module def foo1(): print('foo1 called.') foo1() module.foo2() ⏵ python3 script.py foo1 called. foo2 called. Import by name: ⏵ cat script.py from module import foo2 def foo1(): print('foo1 called.') foo1() foo2() ⏵ python3 script.py foo1 called. foo2 called.
- grawprog 5y ago>python3 script.py I was using Python 2 at the time. Python 3 was still relatively new. Not sure how much difference it makes for your example, but the import systems are different between 2 and 3. https://nerdparadise.com/programming/python/import2vs3 https://nerdparadise.com/programming/python/import2vs3
- markmark 5y agoI moved module.py into a modules folder to clean things up and now I get: "ImportError: attempted relative import with no known parent package" Looks like I'm back to having to learn a bunch of stuff about scripts and modules again?