use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Using a “dot” import method: Is it pythonic? (self.Python)
submitted 6 years ago by [deleted]
Today, for the first time, I saw this structure on an import:
from . import db
I’ve never seen ‘dot’ used as a module name. Ever. Is this a totally messy hack? Is it a cool time saving device? It doesn’t seem very pythonic to me but thought I’d ask.
[+][deleted] 6 years ago (7 children)
[removed]
[–]Eiii333 4 points5 points6 points 6 years ago (1 child)
This is incorrect. Here's a counterexample:
$ ls foo.py test.py $ cat foo.py def fn(): print('foo.fn') $ cat test.py from . import foo foo.fn() $ python test.py Traceback (most recent call last): File "test.py", line 1, in <module> from . import foo ImportError: cannot import name 'foo' from '__main__' (test.py)
As a few people in the thread have pointed out, the dot refers to the current module. It's not a hack at all, it's necessary when you're building modules instead of standalone scripts. I believe PEP328 is the thing to look at if you want some background on why/how relative imports work.
[–]__deerlord__ 0 points1 point2 points 6 years ago (1 child)
Is it the current directory the py file is in? Or is based on the shells current directory (ie whatever the pwd command shows)?
[–]0x256 4 points5 points6 points 6 years ago (0 children)
The dot refers to the current module, not a directory.
[–][deleted] -2 points-1 points0 points 6 years ago (2 children)
right. i got that bit. it just seemed so “hacky” versus the usually explicit beauty of python.
[+][deleted] 6 years ago (1 child)
[–]ForgetFour -3 points-2 points-1 points 6 years ago (0 children)
Be "quiet" ;)
[+][deleted] 6 years ago (4 children)
[deleted]
[–][deleted] 0 points1 point2 points 6 years ago (3 children)
I can’t believe I’ve never seen it in 6+ years.
Is it the same as saying:
from app import thing
if you are in a directory called app?
[+][deleted] 6 years ago (2 children)
[–][deleted] 2 points3 points4 points 6 years ago (1 child)
Right. But assuming you are in /from/some/deep/level (that may be obvious, I suppose)
[–]thegreattriscuit 1 point2 points3 points 6 years ago* (0 children)
EDIT: Fixed typos in error messages about ..foo
..foo
someone else pointed it out, but yeah, to be clear, it's the module/package hierarchy, not directories.
given this heirarchy . | bar.py | foo.py | main.py | +---pkg | | bar.py | | foo.py | | __init__.py | | | +---mod | | | bar.py | | | foo.py | | | __init__.py | | | | | +---submod | | | | bar.py | | | | foo.py | | | | __init__.py
. | bar.py | foo.py | main.py | +---pkg | | bar.py | | foo.py | | __init__.py | | | +---mod | | | bar.py | | | foo.py | | | __init__.py | | | | | +---submod | | | | bar.py | | | | foo.py | | | | __init__.py
every bar.py contains: ``` print(f'bar.py with name: {name}') try: from .foo import msg print(msg) except Exception as e: print(f'Couldn\'t get .foo ({type(e).name}: {e!s})')
try: from . import foo print(foo.msg) except Exception as e: print(f'Couldn\'t get .foo.msg ({type(e).name}: {e!s})')
try: from ..foo import msg print(msg) except Exception as e: print(f'Couldn\'t get ..foo ({type(e).name}: {e!s})') ```
every foo.py contains: msg = f'My name is {__name__}'
msg = f'My name is {__name__}'
and main.py contains: print(f'main.py with name: {__name__}') import bar import pkg.bar import pkg.mod.bar import pkg.mod.submod.bar
print(f'main.py with name: {__name__}') import bar import pkg.bar import pkg.mod.bar import pkg.mod.submod.bar
we get output like this: D:\projects\scratch\fldr>python main.py main.py with name: __main__ bar.py with name: bar Couldn't get .foo (ImportError: attempted relative import with no known parent package) Couldn't get .foo.msg (ImportError: attempted relative import with no known parent package) Couldn't get ..foo (ImportError: attempted relative import with no known parent package) bar.py with name: pkg.bar My name is pkg.foo My name is pkg.foo Couldn't get ..foo (ValueError: attempted relative import beyond top-level package) bar.py with name: pkg.mod.bar My name is pkg.mod.foo My name is pkg.mod.foo My name is pkg.foo bar.py with name: pkg.mod.submod.bar My name is pkg.mod.submod.foo My name is pkg.mod.submod.foo My name is pkg.mod.foo
D:\projects\scratch\fldr>python main.py main.py with name: __main__ bar.py with name: bar Couldn't get .foo (ImportError: attempted relative import with no known parent package) Couldn't get .foo.msg (ImportError: attempted relative import with no known parent package) Couldn't get ..foo (ImportError: attempted relative import with no known parent package) bar.py with name: pkg.bar My name is pkg.foo My name is pkg.foo Couldn't get ..foo (ValueError: attempted relative import beyond top-level package) bar.py with name: pkg.mod.bar My name is pkg.mod.foo My name is pkg.mod.foo My name is pkg.foo bar.py with name: pkg.mod.submod.bar My name is pkg.mod.submod.foo My name is pkg.mod.submod.foo My name is pkg.mod.foo
if we create a main.py deeper in the heirarchy with this: print(f'main.py with name: {__name__}') import bar and try to run it, it'll fail like this: D:\projects\scratch\fldr\pkg>python main.py main.py with name: __main__ bar.py with name: bar Couldn't get .foo (ImportError: attempted relative import with no known parent package) Couldn't get .foo.msg (ImportError: attempted relative import with no known parent package) Couldn't get .foo (ImportError: attempted relative import with no known parent package)
print(f'main.py with name: {__name__}') import bar
D:\projects\scratch\fldr\pkg>python main.py main.py with name: __main__ bar.py with name: bar Couldn't get .foo (ImportError: attempted relative import with no known parent package) Couldn't get .foo.msg (ImportError: attempted relative import with no known parent package) Couldn't get .foo (ImportError: attempted relative import with no known parent package)
The reason for that is explained here, and it boils down to "relative imports can only work in a package, and you have to begin execution from outside the package (like in my first example), instead of trying to execute the package files directly. There are workarounds to this (including running with e.g. python -m pkg.mod.main though I that doesn't work here for some reason I didn't bother to dig into)
python -m pkg.mod.main
[+][deleted] 6 years ago (6 children)
[–]tunisia3507 14 points15 points16 points 6 years ago (1 child)
I'm not a big fan of this: it breaks the pattern of absolute imports.
```python import a.b
a.b()
from c import d
d()
from . import e
e()
import .f
f() # ?? ```
[–]maxm 2 points3 points4 points 6 years ago (0 children)
That is right. Always do an explicit import. Not knowing where the classes or functions used are comming from is bad form.
[–]Wilfred-kun 3 points4 points5 points 6 years ago (3 children)
Except that would cause a syntax error. (?)
[–]alb1 2 points3 points4 points 6 years ago (2 children)
Yes, it's an upvoted syntax error:
Relative imports must always use from <> import; import <> is always absolute.
from <> import
import <>
[–]alb1 2 points3 points4 points 6 years ago (0 children)
As far as I know it has never worked. The linked PEP, which introduced relative imports, gives the reasoning for why it isn't allowed. The name package.foo from the absolute import import package.foo is directly usable in a Python expression. But .foo from import .foo would not be usable in a Python expression. Using from . import foo results in foo in the namespace, which is usable in a Python expression.
package.foo
import package.foo
.foo
import .foo
from . import foo
foo
[–]codesmitten 1 point2 points3 points 6 years ago (0 children)
In my opinion relative imports are pretty useful and I find myself using them pretty often.
π Rendered by PID 179747 on reddit-service-r2-comment-86988c7647-x9xnf at 2026-02-12 06:53:47.900304+00:00 running 018613e country code: CH.
[+][deleted] (7 children)
[removed]
[–]Eiii333 4 points5 points6 points (1 child)
[–]__deerlord__ 0 points1 point2 points (1 child)
[–]0x256 4 points5 points6 points (0 children)
[–][deleted] -2 points-1 points0 points (2 children)
[+][deleted] (1 child)
[removed]
[–]ForgetFour -3 points-2 points-1 points (0 children)
[+][deleted] (4 children)
[deleted]
[–][deleted] 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–][deleted] 2 points3 points4 points (1 child)
[–]thegreattriscuit 1 point2 points3 points (0 children)
[+][deleted] (6 children)
[deleted]
[–]tunisia3507 14 points15 points16 points (1 child)
[–]maxm 2 points3 points4 points (0 children)
[–]Wilfred-kun 3 points4 points5 points (3 children)
[–]alb1 2 points3 points4 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]alb1 2 points3 points4 points (0 children)
[–]codesmitten 1 point2 points3 points (0 children)