2 ms·
Import by module: ⏵ ls module.py script.py ⏵ cat module.py def foo2(): print('foo2 called.') ⏵ cat script.py impor
by mixmastamyk 5y ago
Import 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?