Fixes `ModuleNotFoundError` exceptions
Project description
fiximport
Fixes ModuleNotFoundError
exceptions from absolute imports by automatically adding the entire project to sys.path
.
This is similar to what PyCharm does when a directory is marked as Sources Root
.
Install
pip install fiximport
Use
import fiximport
import mymodule.userutils
import mymodule.config
[...]
fiximport
must come before the absolute imports.fiximport
only needs to be added to files intended to be invoked by the CLI.
Eg,python cool_script.py
.fiximport
is safe and idempotent. You could add it to every file, if you wished.
Sample Project
📁 coolproject
📁 src
📄 weather.py
📁 scripts
📄 printweather.py
Before
from src.weather import get_weather
print(f"The weather today is {get_weather('Bellingham, WA')}")
$ python printweather.py
Traceback (most recent call last):
File "printweather.py", line 1, in <module>
from src.weather import get_weather
ModuleNotFoundError: No module named 'src'
After
import fiximport
from src.weather import get_weather
print(f"The weather today is {get_weather('Bellingham, WA')}")
$ python printweather.py
The weather today is 67°F
Implementation Details
fiximport
identifies the project root by iterating up, until the first "root type files" are found.- If the project root cannot be found heuristically, it defaults to one folder above the python file that called
import fiximport
. - After the root is heuristically found, all folders with a
python
file inside are added tosys.path
. This enables absolute imports to "just work".
This is not a robust permanent solution to python's import system, but it should cover simple projects nicely.
Troubleshooting
- Avoid naming collisions within your project. If there are two folders named
utils
, egfoo1/utils
andfoo2/utils
, add the prefix:from foo2.utils import check_file
. - Avoid naming collisions with common libraries. Eg
math
,statistics
,tempfile
, etc.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
fiximport-1.0.2.tar.gz
(5.0 kB
view details)
Built Distribution
File details
Details for the file fiximport-1.0.2.tar.gz
.
File metadata
- Download URL: fiximport-1.0.2.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 301147640e8134cc0e4e079cef5889a51ce4c2331461a9392f07c3c76957b6b0 |
|
MD5 | fbd2638b15f0b3c2228270a03e3bf5eb |
|
BLAKE2b-256 | e26b19dfe2051aec6592bf441a04e0717a6c9e72e2f6af572f5fac71d94ed74c |
File details
Details for the file fiximport-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: fiximport-1.0.2-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6736b5ab459ae88e8e72072e1be43dea405d451b6e03a3b6cbe91da1fc8ed7d9 |
|
MD5 | e99c20857c80ba0947f8d56cf3f1585c |
|
BLAKE2b-256 | 34fafdddbcca41228facd466877537ad16d95d17a19f1f23109d03ec91cc9602 |