Python Forum
a bunch of modules to import
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a bunch of modules to import
#1
i have a bunch of modules to import. their names are created by some code that uses a number in range(10,301) to make them. what is the most practical and pythonic way to do that? code might look like:
for number in range(10,301):
    x = number * number * number
    z = (x+9999) % 1000 + 137
    name = 'vmod' + str(z)[:3]
    from {name} import calc as {name}
    ...
    ...
...
trouble is, the above code does not work as hoped because variable name does not get substituted.

is there a way to do this on a host with no writable storage space?
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
Hsve you looked at the importlib module: https://docs.python.org/3/library/importlib.html?
Skaperen likes this post
Reply
#3
Use
import importlib
module = importlib.import_module(name)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Different Ways to Import Modules RockBlok 2 539 Dec-11-2023, 04:29 PM
Last Post: deanhystad
  Import Modules TheBunyip 4 1,253 Mar-10-2023, 04:47 PM
Last Post: buran
  How to use Bunch data structure moish 2 2,922 Dec-24-2020, 06:25 PM
Last Post: deanhystad
  Did interpreter 'compile' all import modules(from thrid-party) jamesyuan 10 4,330 Oct-19-2020, 10:49 AM
Last Post: jamesyuan
  Import all items from all modules from package DD169 3 2,124 Jan-25-2020, 12:33 PM
Last Post: buran
  Import Python Modules zowhair 4 2,829 Jun-27-2019, 06:30 AM
Last Post: Gribouillis
  [Jupyter] import notebooks as modules Sirduke1 1 2,679 Aug-20-2018, 08:15 PM
Last Post: perfringo
  Python3: problem to import personals modules PyForIO 2 3,507 Mar-04-2018, 12:42 PM
Last Post: PyForIO
  Is there another way to do a bunch of methods calls ? Windspar 7 4,717 Jan-02-2018, 06:26 PM
Last Post: Windspar
  a module to import modules Skaperen 4 3,906 Aug-05-2017, 09:41 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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