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
#2
I've been told that import * is bad form too. Not sure why, but import file is preferred.

I think you can just put them all in one file and import Weapons then set a variables like:

staff = Simple_Melee.quarterstaff
print(staff)
It's bunch of long names in a dictionary, in four modules. Unless you want to do a bunch of renaming or moving I don't know what you can do.

Unless you are going having billions of weapons you might do well just to have them as their own dictionaries in the weapons file, and use """ or # to separate them for your own sake.

That's likely my route. Just a file called Weapons_dicts.py.
Reply
#3
Quote:I've been told that import * is bad form too.
I've always thought that the objection to this was unfounded. My reasoning:

Theoretical, the theory was that using from module import something
would save processing, however even with the from ... import ... form, the entire package will be read by the interpreter, so if you planed on using more that one method from the package, I see no harm in using import *. (my opinion, not universally accepted)
Reply
#4
(Jan-25-2020, 12:14 PM)Larz60+ Wrote: I've always thought that the objection to this was unfounded.
It was never about processing, but [mainly] effects on the namespace

https://python-forum.io/Thread-Namespace...th-imports

+ it makes difficult to follow what name comes (is imported) from where.

Also the discussion on SO:
https://stackoverflow.com/questions/2386...import-bad

So, don't do it
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Different Ways to Import Modules RockBlok 2 517 Dec-11-2023, 04:29 PM
Last Post: deanhystad
  Import Modules TheBunyip 4 1,229 Mar-10-2023, 04:47 PM
Last Post: buran
  a bunch of modules to import Skaperen 2 884 Nov-07-2022, 07:33 PM
Last Post: Gribouillis
  Unable to import sklearn after installing any package ilango 0 1,184 Oct-25-2021, 07:03 AM
Last Post: ilango
  Can't import package hubenhau 1 5,934 Jan-15-2021, 06:23 PM
Last Post: buran
  Did interpreter 'compile' all import modules(from thrid-party) jamesyuan 10 4,279 Oct-19-2020, 10:49 AM
Last Post: jamesyuan
  import scalalib package doesn't work manu_brighter 2 2,891 Apr-17-2020, 06:36 PM
Last Post: snippsat
  Importing module from a package results in import error goghvv 2 2,383 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,766 Jul-19-2019, 04:58 PM
Last Post: snippsat
  Import Python Modules zowhair 4 2,797 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