- I attempt to import a Python module that’s in a subdirectory of my
current working directory. - If I name the subdirectory
foo_bar
orone_two_three
orpython_foo
, the import succeeds. - But if I name the subdirectory
python_utils
, the import fails:
<code>02:27: droot ⦿ tree
.
└── python_utils
└── dostuff.py
2 directories, 1 file
02:27: droot ⦿ python
Python 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from python_utils.dostuff import foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'python_utils.dostuff'
>>>
02:27: droot ⦿ mv python_utils/ foo_bar/
02:27: droot ⦿ python
Python 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from foo_bar.dostuff import foo
>>>
02:28: droot ⦿ mv foo_bar/ one_two_three/
02:29: droot ⦿ python
Python 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from one_two_three.dostuff import foo
>>>
02:35: droot ⦿ mv one_two_three/ python_foo
02:37: droot ⦿python
Python 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from python_foo.dostuff import foo
>>>
</code>
<code>02:27: droot ⦿ tree
.
└── python_utils
└── dostuff.py
2 directories, 1 file
02:27: droot ⦿ python
Python 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from python_utils.dostuff import foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'python_utils.dostuff'
>>>
02:27: droot ⦿ mv python_utils/ foo_bar/
02:27: droot ⦿ python
Python 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from foo_bar.dostuff import foo
>>>
02:28: droot ⦿ mv foo_bar/ one_two_three/
02:29: droot ⦿ python
Python 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from one_two_three.dostuff import foo
>>>
02:35: droot ⦿ mv one_two_three/ python_foo
02:37: droot ⦿python
Python 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from python_foo.dostuff import foo
>>>
</code>
02:27: droot ⦿ tree
.
└── python_utils
└── dostuff.py
2 directories, 1 file
02:27: droot ⦿ python
Python 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from python_utils.dostuff import foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'python_utils.dostuff'
>>>
02:27: droot ⦿ mv python_utils/ foo_bar/
02:27: droot ⦿ python
Python 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from foo_bar.dostuff import foo
>>>
02:28: droot ⦿ mv foo_bar/ one_two_three/
02:29: droot ⦿ python
Python 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from one_two_three.dostuff import foo
>>>
02:35: droot ⦿ mv one_two_three/ python_foo
02:37: droot ⦿python
Python 3.12.3 (main, Apr 17 2024, 00:00:00) [GCC 14.0.1 20240411 (Red Hat 14.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from python_foo.dostuff import foo
>>>
- What’s going on? I have tried many names, and all work except
python_utils
!