Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 51 characters in body
Source Link

Here is a dead simple solution in case you don't want to do any of these:

  • add __init__.py files
  • run with python -m mymodule
  • edit __package__
  • add if check in __main__
  • edit sys.path by 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__.py files
  • run with python -m mymodule
  • edit __package__
  • add if check in __main__
  • edit sys.path by 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__.py files
  • run with python -m mymodule
  • edit __package__
  • add if check in __main__
  • edit sys.path by 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.

added 82 characters in body
Source Link

Here is a dead simple solution in case you don't want to do any of these:

  • add __init__.py files
  • run with python -m mymodule
  • edit __package__
  • add if check in __main__
  • edit sys.path by 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__.py files
  • run with python -m mymodule
  • edit __package__
  • add if check in __main__
  • edit sys.path by hand
  • edit PYTHONPATH
  • restructure the project

pip install importmonkey

[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__.py files
  • run with python -m mymodule
  • edit __package__
  • add if check in __main__
  • edit sys.path by 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.

Source Link

Here is a dead simple solution in case you don't want to do any of these:

  • add __init__.py files
  • run with python -m mymodule
  • edit __package__
  • add if check in __main__
  • edit sys.path by hand
  • edit PYTHONPATH
  • restructure the project

pip install importmonkey

[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.

lang-py

AltStyle によって変換されたページ (->オリジナル) /