This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Package Relative Imports - double dot doc example not working
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.9
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Ixio, brett.cannon, docs@python, eric.snow, gvanrossum, ncoghlan
Priority: normal Keywords:

Created on 2021-01-22 23:22 by Ixio, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg385515 - (view) Author: (Ixio) Date: 2021-01-22 23:22
I've been trying to do a double dot import without success, I've tried going back to the official documentation in order to figure it out however even the example shown does not work.

Official example :

The following instructions mimic pretty well the doc example :

mkdir package
cd package
mkdir subpackage1
touch subpackage1/
mkdir subpackage2
touch subpackage2/
echo "eggs = 42" > subpackage2/
echo "from ..subpackage2.moduleZ import eggs" > subpackage1/
python subpackage1/
cd subpackage1

cd ..
echo "from subpackage1 import moduleX" >

However I get 3 times the following error :
    from ..subpackage2.moduleZ import eggs
ImportError: attempted relative import beyond top-level package

Even though the docs say "In [...] subpackage1/ [...] the following are valid relative imports: [...] from ..subpackage2.moduleZ import eggs".

I have no idea how to fix the doc as I would love to do "from ..subpackage2.moduleZ import eggs" from "subpackage1/" since that's the structure I'm trying to set up for a Python project of mine but obviously it does not seem to work.

My system uses Python 3.9 and I've used Docker to try this scenario on Python 3.8, 3.5 and even 3.2: it does not work either. I've also tried on another machine and asked a friend to try, no luck.

I'm hoping someone here can explain how double dot imports work and fix the documentation accordingly.
msg385516 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-01-22 23:32
This is not a bug in Python. Please ask a user forum for help understanding (for example,
Date User Action Args
2022-04-11 14:59:40adminsetgithub: 87171
2021-01-22 23:32:43gvanrossumsetstatus: open -> closed

nosy: + gvanrossum
messages: + msg385516

resolution: not a bug
stage: resolved
2021-01-22 23:22:37Ixiocreate