Skip to main content

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

  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.

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.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

fiximport-1.0.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file fiximport-1.0.1.tar.gz.

File metadata

  • Download URL: fiximport-1.0.1.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

Hashes for fiximport-1.0.1.tar.gz
Algorithm Hash digest
SHA256 92d6a31c9815bbaa9a3abe2090b7fc4fa9a2d7301cca032f52934904fb519d83
MD5 9bc52d7d027562b12e9629f398e3cbb8
BLAKE2b-256 4667e93c160a8fa2b951702bad7669c613ce913feeb5f9f658a416d4b94d03d0

See more details on using hashes here.

File details

Details for the file fiximport-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: fiximport-1.0.1-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

Hashes for fiximport-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 014d0e93dadd0576b4c3b124f81e7684a34c2fc772ac886383ff35e9ff16eaf5
MD5 cf42d1bf2a96698032cfacd04bc3a536
BLAKE2b-256 4f990cdf13a8fb535cea4286cb070de0bec136ea53f9c92ffd23f3b921c4d182

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page