Python Forum
Import all items from all modules from package
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Import all items from all modules from package
#1
Hello,

File Structure:
DND:
  • testing.py
  • Weapons:
    • __init__.py
    • Simple_Melee.py
    • Simple_Ranged.py
    • Martial_Melee.py
    • Martial_Ranged.py
I have four python files containing several dictionaries. In a file outside of the folder containing the four files (testing.py) I want to have access to those dictionaries. For example, in Simple_Melee.py there is a dictionary called 'quarterstaff'. In the testing.py file, I want to be able to call print(quarterstaff) NOT print(Simple_Melee.quarterstaff).

Simple_Melee.py:
quarterstaff = {
    'damage': '1d6',
    'versatile_damage': '1d8'
}
testing.py:
from Weapons import *
# print(quarterstaff)  # Doesn't work
print(Simple_Melee.quarterstaff)  # Does work, but not what I want

from Weapons.Simple_Melee   import *
from Weapons.Simple_Ranged  import *
from Weapons.Martial_Melee  import *
from Weapons.Martial_Ranged import *
print(quarterstaff)  # Works as I want, but I have to have 4 import statements to get all weapons loaded
How do I import all dictionaries (weapons) into testing.py? Or more concisely, is there a better way than four import statements? If not, then the next best thing will be to move all of the weapons into the same file, but I like having things better organized than that. I need to access these dictionaries by name, not by module.name.

I like the structure that I am currently using, but I am relatively new to these more advanced file structurings. If there is a better way, feel free to explain that instead of this, because I know that import * is a bad idea.

Thank you,
- DD169
Reply


Messages In This Thread
Import all items from all modules from package - by DD169 - Jan-25-2020, 06:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Different Ways to Import Modules RockBlok 2 562 Dec-11-2023, 04:29 PM
Last Post: deanhystad
  Import Modules TheBunyip 4 1,275 Mar-10-2023, 04:47 PM
Last Post: buran
  a bunch of modules to import Skaperen 2 918 Nov-07-2022, 07:33 PM
Last Post: Gribouillis
  Unable to import sklearn after installing any package ilango 0 1,209 Oct-25-2021, 07:03 AM
Last Post: ilango
  Can't import package hubenhau 1 6,007 Jan-15-2021, 06:23 PM
Last Post: buran
  Did interpreter 'compile' all import modules(from thrid-party) jamesyuan 10 4,419 Oct-19-2020, 10:49 AM
Last Post: jamesyuan
  import scalalib package doesn't work manu_brighter 2 2,931 Apr-17-2020, 06:36 PM
Last Post: snippsat
  Importing module from a package results in import error goghvv 2 2,424 Mar-27-2020, 07:13 PM
Last Post: goghvv
  Can't decide - install modules with Ubuntu package manager (apt) or pip3 in venv Thisisaline 6 3,859 Jul-19-2019, 04:58 PM
Last Post: snippsat
  Import Python Modules zowhair 4 2,858 Jun-27-2019, 06:30 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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