Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a module to import modules
#1
i am working on a large program that is made of many files the get run as separate processes.  there is a big set of modules that every file needs to import.  i hate having this list duplicate in every file, especially when i need to make changes.  in C i would have one #include file the would #include all the other #include files.  then each C file would #include that one #include file.

what's a good way (or best way) to do this with large projects in Python?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Maybe a sub-module with all the other files?  In an __init__.py file, you can do all the importing, and optionally have an __all__ defined, so wherever you want those modules, you can do from modules import * and have a very well defined list of what, exactly, is being imported?
Reply
#3
so if i use the filename __init__.py i don't need to actually import it?  i figured i would need to do from mycommonmodulename import *.  would that be enough?  can i do from whatever import * there, too (not the same names, of course)?

i was planning on using the name "common".  a choice i have used since my Fortran days (on mainframes, before i switched to Assembler for everything, which was before i did C, which was before i am doing Python).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
If you have a folder named "common", and that folder has a file in it named "__init__.py", then you can do import common or from common import *, and the __init__.py will be run.  Inside that file, you can then do all the common importing as a convenience.

Defining __all__ in the top level just determines what, exactly, is imported if you do from common import *, so that import * doesn't HAVE to actually import everything, only the things you actually want exposed outside the module.
Reply
#5
here is a real world example of using dunder init file in a complex project

the import
https://github.com/metulburr/pyroller/bl...ain.py#L14

craps directory content of __init__.py
https://github.com/metulburr/pyroller/bl..._init__.py
It only imports the Craps class in /craps/craps.py but you can add any number of imports to the __init__.py file.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Different Ways to Import Modules RockBlok 2 513 Dec-11-2023, 04:29 PM
Last Post: deanhystad
  is import cointegration_analysis a recognized module mitcht33 1 424 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  problem in import module from other folder akbarza 5 1,388 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 712 Aug-06-2023, 01:09 AM
Last Post: aupres
  Import Modules TheBunyip 4 1,218 Mar-10-2023, 04:47 PM
Last Post: buran
  import module error tantony 5 3,434 Dec-15-2022, 01:55 PM
Last Post: Lauraburmrs
  a bunch of modules to import Skaperen 2 880 Nov-07-2022, 07:33 PM
Last Post: Gribouillis
  Import a module one step back of the path prathampatel9 1 1,067 Sep-21-2022, 01:34 PM
Last Post: snippsat
  Import a module for use in type hint? Milosz 0 1,478 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Can't install nor import delorean module Tek 3 2,793 Oct-27-2021, 03:32 AM
Last Post: Tek

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020