python relative import

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2020-10-20 01:00:00 +00:00
parent 27fc5deefe
commit 3990333f3d
8 changed files with 51 additions and 0 deletions

View File

@@ -0,0 +1 @@
https://cirosantilli.com/linux-kernel-module-cheat#python-relative-imports

View File

@@ -0,0 +1,7 @@
#!/usr/bin/env python
import myfile
assert myfile.a == 1
b = myfile.a + 1

View File

@@ -0,0 +1 @@
a = 1

View File

@@ -0,0 +1,7 @@
#!/usr/bin/env python
import myfile
import mydir.myfile_user
assert myfile.a == 1
assert mydir.myfile_user.b == 2

View File

@@ -0,0 +1,20 @@
#!/usr/bin/env bash
set -eux
# myfile_user works from all directories.
./myfile_user.py
cd ..
relative_import/myfile_user.py
cd relative_import/mydir
../myfile_user.py
cd ..
# TODO any better way to run mydir/myfile_user.py?
# ./mydir/myfile_user.py
#Traceback (most recent call last):
# File "./mydir/myfile_user.py", line 3, in <module>
# import myfile
#ModuleNotFoundError: No module named 'myfile'
python -m mydir.myfile_user