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
32
votes
2 answers

How is the __name__ variable in a Python module defined?

I'm aware of the standard example: if you execute a module directly then it's __name__ global variable is defined as "__main__". However, nowhere in the documentation can I find a precise description of how __name__ is defined in the general case.…
Jeremy
  • 1
  • 85
  • 340
  • 366
31
votes
1 answer

Why do I need __init__.py at every level?

Given that I have the following directory structure with . being the current working directory . \---foo \---bar \---__init__.py \---baz.py When I run python -c "import foo.bar.baz" I get Traceback (most recent call last): …
quittle
  • 856
  • 2
  • 10
  • 19
31
votes
3 answers

Importing everything ( * ) dynamically from a module

I have a Python module that I want to dynamically import given only a string of the module name. Normally I use importlib or __import__ and this works quite well given that I know which objects I want to import from the module, but is there a way to…
Stephen Diehl
  • 8,271
  • 5
  • 38
  • 56
30
votes
4 answers

ImportError: cannot import name 'Iterable' from 'collections' in Python

Working in Python with Atom on a Mac. Code: from rubik.cube import Cube from rubik_solver import utils Full error: Traceback (most recent call last): File "/Users/Audey/Desktop/solver.py", line 2, in from rubik_solver import utils …
Myexgiko
  • 423
  • 1
  • 4
  • 12
30
votes
2 answers

How to Import python package from another directory?

I have a project that is structured as follows: project ├── api │ ├── __init__.py │ └── api.py ├── instance │ ├── __init__.py │ └── config.py ├── package │ ├── __init__.py │ └── app.py ├── requirements.txt └── tests └──…
csymvoul
  • 677
  • 3
  • 15
  • 30
30
votes
7 answers

How to move all modules to new version of Python (from 3.6 to 3.7)

I just upgraded to python 3.7 and I realized that all my modules stuck with the previous version. Even Django is not recognised anymore. How can I do to transfer everything to the new version? I am a little lost right now, don't even know where the…
NicoBar
  • 555
  • 1
  • 7
  • 16
30
votes
3 answers

Python: sharing common code among a family of scripts

I'm writing a family of Python scripts within a project; each script is within a subdirectory of the project, like so: projectroot | |- subproject1 | | | |- script1.main.py | `- script1.merger.py | |- subproject2 | | | …
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
27
votes
2 answers

pyspark import user defined module or .py files

I built a python module and I want to import it in my pyspark application. My package directory structure is: wesam/ |-- data.py `-- __init__.py A simple import wesam at the top of my pyspark script leads to ImportError: No module named wesam. I…
Sam
  • 11,799
  • 9
  • 49
  • 68
27
votes
1 answer

Reload a Module in Python 3.4

I know this might sound like a really stupid question but whatever. I've made a small script in Python and I've made some changes while in a shell. Normally, on an OS X computer (It's running Python 2.7), I would simply type in reload(the_module)…
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
27
votes
3 answers

Django 1.7 conflicting models

I install my application in "project/apps/myapp" folder. Both apps and myapp folders have init.py files(Without any of them there is module missing error). Now I've the error: Exception Type: RuntimeError at / Exception Value: Conflicting…
Crampus
  • 491
  • 1
  • 8
  • 16
26
votes
6 answers

How to properly use relative or absolute imports in Python modules?

Usage of relative imports in Python has one drawback, you will not be able to run the modules as standalones anymore because you will get an exception: ValueError: Attempted relative import in non-package # /test.py: just a sample file importing foo…
sorin
  • 161,544
  • 178
  • 535
  • 806
26
votes
6 answers

A plethora of Python OSC modules - which one to use?

Open Sound Control (OSC) is a protocol for communication among computers, sound synthesizers, and other multimedia devices that is optimized for modern networking technology. It is particularly common to use OSC with MAX/MSP -- which in fact is what…
Wes Modes
  • 2,024
  • 2
  • 22
  • 40
24
votes
1 answer

How do I use importlib.LazyLoader?

In my module, I have a couple of functions that depend on an external module with a long startup time. How do I use LazyLoader? If I have import veggies or import veggies.brussels.sprouts or from veggies.brussels import sprouts how would I…
gerrit
  • 24,025
  • 17
  • 97
  • 170
23
votes
1 answer

What is the difference between Python's unittest and unittest2 modules?

I currently work on some code that uses unittest2 module. I suspect this code was meant for the python2. Can one use python3 unittest as a drop in replacement for unittest2? What is the difference between the two?
23
votes
2 answers

Easiest way to automatically download required modules in Python?

I would like to release a python module I wrote which depends on several packages. What's the easiest way to make it so these packages are programmatically downloaded just in case they are not available on the system that's being run? Most of these…
user248237