Questions tagged [python-module]

A module is a file containing Python definitions and statements.

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

2038 questions
86
votes
5 answers

What's the difference between __builtin__ and __builtins__?

I was coding today and noticed something. If I open a new interpreter session (IDLE) and check what's defined with the dir function I get this: $ python >>> dir() ['__builtins__', '__doc__', '__name__', '__package__'] >>>…
santiagobasulto
  • 11,320
  • 11
  • 64
  • 88
84
votes
6 answers

How do I document a module in Python?

That's it. If you want to document a function or a class, you put a string just after the definition. For instance: def foo(): """This function does nothing.""" pass But what about a module? How can I document what a file.py does?
Auron
  • 13,626
  • 15
  • 47
  • 54
82
votes
6 answers

How to get filename of the __main__ module in Python?

Suppose I have two modules: a.py: import b print __name__, __file__ b.py: print __name__, __file__ I run the "a.py" file. This prints: b C:\path\to\code\b.py __main__ C:\path\to\code\a.py Question: how do I obtain the path to the __main__…
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
82
votes
2 answers

Two Python modules require each other's contents - can that work?

I have a Bottle webserver module with the following line: from foobar.formtools import auto_process_form_insert And the foobar.formtools module contains this line: from foobar.webserver import redirect, redirect_back Of course, both result in the…
Hubro
  • 56,214
  • 69
  • 228
  • 381
79
votes
4 answers

What is Python's heapq module?

I tried "heapq" and arrived at the conclusion that my expectations differ from what I see on the screen. I need somebody to explain how it works and where it can be useful. From the book Python Module of the Week under paragraph 2.2 Sorting it is…
minerals
  • 6,090
  • 17
  • 62
  • 107
77
votes
8 answers

Prevent Python from caching the imported modules

While developing a largeish project (split in several files and folders) in Python with IPython, I run into the trouble of cached imported modules. The problem is that instructions import module only reads the module once, even if that module has…
Olivier Verdier
  • 46,998
  • 29
  • 98
  • 90
74
votes
4 answers

How does python find a module file if the import statement only contains the filename?

Everywhere I see Python code importing modules using import sys or import mymodule How does the interpreter find the correct file if no directory or path is provided?
Asciiom
  • 9,867
  • 7
  • 38
  • 57
72
votes
10 answers

Python auto import extension for VSCode

Is there a Python auto import extension/plugin available for VSCode? By auto import I mean, auto import of python modules. Eclipse and Intellij has this feature with Java.
68
votes
5 answers

Python: 'Private' module in a package

I have a package mypack with modules mod_a and mod_b in it. I intend the package itself and mod_a to be imported freely: import mypack import mypack.mod_a However, I'd like to keep mod_b for the exclusive use of mypack. That's because it exists…
G S
  • 35,511
  • 22
  • 84
  • 118
67
votes
12 answers

How to check if a module is installed in Python and, if not, install it within the code?

I would like to install the modules 'mutagen' and 'gTTS' for my code, but I want to have it so it will install the modules on every computer that doesn't have them, but it won't try to install them if they're already installed. I currently have: def…
Foxes
  • 1,137
  • 3
  • 10
  • 19
64
votes
8 answers

How to reload python module imported using `from module import *`

I saw in this useful Q&A that one can use reload(whatever_module) or, in Python 3, imp.reload(whatever_module). My question is, what if I had said from whatever_module import * to import? Then I have no whatever_module to refer to when I use…
murftown
  • 1,287
  • 1
  • 10
  • 13
61
votes
3 answers

How to import a module from a different folder?

I have a project which I want to structure like this: myproject ├── api │ ├── __init__.py │ └── api.py ├── backend │ ├── __init__.py │ └── backend.py ├── models │ ├── __init__.py │ └── some_model.py └── __init__.py Now, I want to import…
Gasp0de
  • 1,199
  • 2
  • 12
  • 30
61
votes
3 answers

Error while finding spec for 'fibo.py' (: 'module' object has no attribute '__path__')

I have a module in a fibo.py file which has the following functions - #fibonacci numbers module def fib(n): # write Fibonacci series up to n a, b = 0, 1 while b < n: print(b, end=' ') a, b = b, a+b print() def…
Eternal Learner
  • 3,800
  • 13
  • 48
  • 78
58
votes
4 answers

Import python module NOT on path

I have a module foo, containing util.py and bar.py. I want to import it in IDLE or python session. How do I go about this? I could find no documentation on how to import modules not in the current directory or the default python PATH. After trying…
Vort3x
  • 1,778
  • 2
  • 20
  • 37
55
votes
5 answers

Difference between Module and Class in Python

Can I assign value to a variable in the module? If yes, what is the difference between a class and module? PS: I'm a Java guy (in case it helps in the way of explaining). Thanks.
liwevire
  • 782
  • 2
  • 9
  • 21