Sibling package imports

attempted relative import beyond top-level package
attempted relative import with no known parent package
python relative import
modulenotfounderror: no module named python 3
python no module named same directory
python import
python cannot import module
realpython import

I've tried reading through questions about sibling imports and even the package documentation, but I've yet to find an answer.

With the following structure:

├── LICENSE.md
├── README.md
├── api
│   ├── __init__.py
│   ├── api.py
│   └── api_key.py
├── examples
│   ├── __init__.py
│   ├── example_one.py
│   └── example_two.py
└── tests
│   ├── __init__.py
│   └── test_one.py

How can the scripts in the examples and tests directories import from the api module and be run from the commandline?

Also, I'd like to avoid the ugly sys.path.insert hack for every file. Surely this can be done in Python, right?

Hitchhiker's guide to the Python imports, Import sibling packages. And the last thing I want to cover is the import of sibling packages. Suppose we have a sibling package pizzashop : For siblings package imports, you can use either the insert or the append method of the [sys.path][2] module: if __name__ == '__main__' and if __package__ is None: import sys from os import path sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) ) import api

Python3 import and project layout · Gaoping Huang's Blog, We can import from each package as below. from sub1.helper Sometimes, we may want to import a module/method from a sibling directory. What's the correct way to organize this package so that sibling modules can import functions from each other? Answer 1 To make mp.plot.plot_signal() work, you must import plot in the myPackage.__init__.py. Another thing is that in both plot.py and signal.py you should import the whole module to avoid circular imports:

Here is another alternative that I insert at top of the Python files in tests folder:

# Path hack.
import sys, os
sys.path.insert(0, os.path.abspath('..'))

Asking for Help/How can I import a module from a sibling directory , Asking for Help: How can I import a module from a sibling directory? I am new to Python from Java background, I have following structure. Put this snippet at the top of any Python file that is not the top level of your project to import to call code from other folders. Snippet if __name__ == '__main__': import os import sys sys.path.append(os.getcwd()) Purpose. This project is intended as an example of how to import sibling packages when developing in Python.

You don't need and shouldn't hack sys.path unless it is necessary and in this case it is not. Use:

import api.api_key # in tests, examples

Run from the project directory: python -m tests.test_one.

You should probably move tests (if they are api's unittests) inside api and run python -m api.test to run all tests (assuming there is __main__.py) or python -m api.test.test_one to run test_one instead.

You could also remove __init__.py from examples (it is not a Python package) and run the examples in a virtualenv where api is installed e.g., pip install -e . in a virtualenv would install inplace api package if you have proper setup.py.

python Sibling package imports?, import sys.path import os.path # Import from sibling directory . ugly hacks to allow imports from siblings modules or parents package from a __main__ module. Put this snippet at the top of any Python file that is not the top level of your project to import to call code from other folders. This project is intended as an example of how to import sibling packages when developing in Python. It is based on 3.6.8, I believe it should work on anything above

I don't yet have the comprehension of Pythonology necessary to see the intended way of sharing code amongst unrelated projects without a sibling/relative import hack. Until that day, this is my solution. For examples or tests to import stuff from ..\api, it would look like:

import sys.path
import os.path
# Import from sibling directory ..\api
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/..")
import api.api
import api.api_key

Absolute vs Relative Imports in Python – Real Python, You can import both packages and modules. (Note that importing a package essentially imports the package's __init__.py file as a module.) You can also import  Remember that package is a namespace for modules, so you don’t import the package itself, you import a module from a package. >>> import pizzapy.menu pizza.py module name is pizza >>> pizzapy.menu.MENU [<pizza.Pizza object at 0x7fa065291160>, <pizza.Pizza object at 0x7fa065291198>, <pizza.Pizza object at 0x7fa065291a20>]

accesing modules in sibling packages - Python, accesing modules in sibling packages. Python Forums on Bytes. Expand|​Select|Wrap|Line Numbers. import test.two; print two.v. the code in  from.some_module import some_class from..some_package import some_function from. import some_class You can see that there is at least one dot in each import statement above. Relative imports make use of dot notation to specify location.

Importing Sibling Packages in Python, I've put together a snippet of code to demonstrate how to import sibling packages and modules in Python 3, with a working example out on  A relative import uses the relative path (starting from the path of the current module) to the desired module to import. There are two types of relative imports: an explicit relative import follows the format from .<module/package> import X, where <module/package> is prefixed by dots .

Python for the Lab Complete Guide to Imports in Python: Absolute , by Aquiles Carattino Oct. 4, 2019 Package absolute importing import relative. Importing is What happens if we want to import from a sibling module? Imagine​  Importing Sibling Packages in Python By Aaron Grossman • August 04, 2019 • 0 Comments I've put together a snippet of code to demonstrate how to import sibling packages and modules in Python 3, with a working example out on GitHub as python-sibling-import-example .

Comments
  • I recommend skipping past all the sys.path hacks and reading the only actual solution that's been posted thus far (after 7 years!).
  • By the way, there's still room for another good solution: Separating executable code from library code; most of the time a script inside a package shouldn't be executable to begin with.
  • This is so helpful, both the question and the answers. I am just curious, how come "Accepted Answer" is not the same as the one awarded the bounty in this case?
  • @Aran-Fey That's an underrated reminder in these relative import error Q&As. I've been looking for a hack this whole time, but deep down I knew there was a simple way to design my way out of the problem. Not to say that it's the solution for everyone here reading, but it's a good reminder as it could be for many.
  • you don't have to if you run from a project directory using -m form or if you install the package (pip and virtualenv make it easy)
  • How does pytest find the api package for you? Amusingly, I found this thread because I'm running into this problem specifically with pytest and sibling package importing.
  • I have two questions, please. 1. Your pattern seems to work without __package__ = "examples" for me. Why do you use it? 2. In what situation is __name__ == "__main__" but __package__ is not None?
  • Thanks for the detailed post. Here is my problem. If I do everything you said and I do a pip freeze, I get a line -e git+https://username@bitbucket.org/folder/myproject.git@f65466656XXXXX#egg=myproject Any Idea on how to resolve?
  • Why doesn't the relative import solution work? I believe you, but I'm trying to understand Python's convoluted system.
  • I have not digged into this so deeply to understand why it does not work; I just see that relative imports beyond top-level package are not allowed. I have also noticed that making pip-installable packages out of my python packages/apps have made my python experience better. But that's of course very subjective :)