Skip to main content

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

  1. fiximport identifies the project root by iterating up, until the first "root type files" are found.
  2. If the project root cannot be found heuristically, it defaults to one folder above the python file that called import fiximport.
  3. After the root is heuristically found, all folders with a python file inside are added to sys.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, eg foo1/utils and foo2/utils, add the prefix: from foo2.utils import check_file.
  • Avoid naming collisions with common libraries. Eg math, statistics, tempfile, etc.

Metadata

Release files for fiximport 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fiximport 1.0.3
File Size Uploaded Provenance Actions
fiximport-1.0.3.tar.gz 5.2 kB Without provenance Details for fiximport-1.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fiximport 1.0.3
File Interpreter ABI Platform Actions
fiximport-1.0.3-py3-none-any.whl Python 3 none any Details for fiximport-1.0.3-py3-none-any.whl

Total release size: 10.0 kB

Release files / fiximport-1.0.3.tar.gz

Download URL fiximport-1.0.3.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
198a0c3dfecb2c243634a23141d9c824b2ee32b31425a88c808709734a94c737
BLAKE2b-256 checksum
How to use checksums
4d17496234703a808d10adf8ac061e7aa2c8c6af217e0fbff59d08853d082685
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Release files / fiximport-1.0.3-py3-none-any.whl

Download URL fiximport-1.0.3-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ca08a895e776bb68f42fd491d8a540b6d746b7586244391436aeb9e56486e6e9
BLAKE2b-256 checksum
How to use checksums
434321cb8bc50350cd62d527c3df71615ab7b06537fc7f7072791c18592a5b4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Security for fiximport 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page