Here is a dead simple solution in case you don't want to do any of these:
- add
__init__.pyfiles - run with
python -m mymodule - edit
__package__ - add
ifcheck in__main__ - edit
sys.pathby hand - edit
PYTHONPATH - restructure the project
pip install importmonkey
This is a robust wrapper around sys.path hacks to keep everything simple and neat.
[github] [pip] [docs]
├─ src
│ └─ project
│ └─ mymodule.py
└─ test
└─ test.py
# In test.py
from importmonkey import add_path
add_path("../src/project") # relative to current __file__
import mymodule
# add as many paths as needed, absolute or relative
# unix path conventions work so you can use '..' and '.'
# add_path validates the paths and returns added path as string
The module does not need to have __init__.py associated with it for this to work.
Disclosure of affiliation: I made importmonkey.
Here is a dead simple solution in case you don't want to do any of these:
- add
__init__.pyfiles - run with
python -m mymodule - edit
__package__ - add
ifcheck in__main__ - edit
sys.pathby hand - edit
PYTHONPATH - restructure the project
pip install importmonkey
This is a robust wrapper around sys.path hacks to keep everything simple and neat.
[github] [pip] [docs]
├─ src
│ └─ project
│ └─ mymodule.py
└─ test
└─ test.py
# In test.py
from importmonkey import add_path
add_path("../src/project") # relative to current __file__
import mymodule
# add as many paths as needed, absolute or relative
# unix path conventions work so you can use '..' and '.'
# add_path validates the paths and returns added path as string
The module does not need to have __init__.py associated with it for this to work.
Here is a dead simple solution in case you don't want to do any of these:
- add
__init__.pyfiles - run with
python -m mymodule - edit
__package__ - add
ifcheck in__main__ - edit
sys.pathby hand - edit
PYTHONPATH - restructure the project
pip install importmonkey
This is a robust wrapper around sys.path hacks to keep everything simple and neat.
[github] [pip] [docs]
├─ src
│ └─ project
│ └─ mymodule.py
└─ test
└─ test.py
# In test.py
from importmonkey import add_path
add_path("../src/project") # relative to current __file__
import mymodule
# add as many paths as needed, absolute or relative
# unix path conventions work so you can use '..' and '.'
# add_path validates the paths and returns added path as string
The module does not need to have __init__.py associated with it for this to work.
Disclosure of affiliation: I made importmonkey.
Here is a dead simple solution in case you don't want to do any of these:
- add
__init__.pyfiles - run with
python -m mymodule - edit
__package__ - add
ifcheck in__main__ - edit
sys.pathby hand - edit
PYTHONPATH - restructure the project
pip install importmonkey
[This is a robust wrapper around sys.path hacks to keep everything simple and neat.
[github] [pip] [docs]
├─ src
│ └─ project
│ └─ mymodule.py
└─ test
└─ test.py
# In test.py
from importmonkey import add_path
add_path("../src/project") # relative to current __file__
import mymodule
# add as many paths as needed, absolute or relative
# unix path conventions work so you can use '..' and '.'
# add_path validates the paths and returns added path as string
The module does not need to have __init__.py associated with it for this to work.
Here is a dead simple solution in case you don't want to do any of these:
- add
__init__.pyfiles - run with
python -m mymodule - edit
__package__ - add
ifcheck in__main__ - edit
sys.pathby hand - edit
PYTHONPATH - restructure the project
pip install importmonkey
├─ src
│ └─ project
│ └─ mymodule.py
└─ test
└─ test.py
# In test.py
from importmonkey import add_path
add_path("../src/project") # relative to current __file__
import mymodule
# add as many paths as needed, absolute or relative
# unix path conventions work so you can use '..' and '.'
# add_path validates the paths and returns added path as string
The module does not need to have __init__.py associated with it for this to work.
Here is a dead simple solution in case you don't want to do any of these:
- add
__init__.pyfiles - run with
python -m mymodule - edit
__package__ - add
ifcheck in__main__ - edit
sys.pathby hand - edit
PYTHONPATH - restructure the project
pip install importmonkey
This is a robust wrapper around sys.path hacks to keep everything simple and neat.
[github] [pip] [docs]
├─ src
│ └─ project
│ └─ mymodule.py
└─ test
└─ test.py
# In test.py
from importmonkey import add_path
add_path("../src/project") # relative to current __file__
import mymodule
# add as many paths as needed, absolute or relative
# unix path conventions work so you can use '..' and '.'
# add_path validates the paths and returns added path as string
The module does not need to have __init__.py associated with it for this to work.
Here is a dead simple solution in case you don't want to do any of these:
- add
__init__.pyfiles - run with
python -m mymodule - edit
__package__ - add
ifcheck in__main__ - edit
sys.pathby hand - edit
PYTHONPATH - restructure the project
pip install importmonkey
├─ src
│ └─ project
│ └─ mymodule.py
└─ test
└─ test.py
# In test.py
from importmonkey import add_path
add_path("../src/project") # relative to current __file__
import mymodule
# add as many paths as needed, absolute or relative
# unix path conventions work so you can use '..' and '.'
# add_path validates the paths and returns added path as string
The module does not need to have __init__.py associated with it for this to work.