Python Forum
making a module mimic another module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
making a module mimic another module
#2
You can create a new module by using the constructor types.ModuleType()
>>> import os
>>> import types
>>> m = types.ModuleType('OS')
>>> for k in dir(os):
...     setattr(m, k.upper(), getattr(os, k))
... 
>>> m.CHDIR
<built-in function chdir>
>>> import sys
>>> sys.modules[m.__name__] = m # optional, if you want to make it importable
>>> import OS
>>> OS is m
True
Reply


Messages In This Thread
making a module mimic another module - by Skaperen - Nov-20-2019, 12:41 AM
RE: making a module mimic another module - by Gribouillis - Nov-20-2019, 05:10 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  importing a module given a path to the file Skaperen 8 1,449 Nov-04-2023, 07:03 PM
Last Post: Skaperen
  where module is loaded from Skaperen 1 779 Jul-26-2023, 05:38 AM
Last Post: Gribouillis
  looking fo documentation for module files Skaperen 39 102,050 Aug-09-2022, 07:37 AM
Last Post: Gribouillis
  Mixed types of numeric data in the statistics module stevendaprano 2 1,331 May-23-2022, 02:15 AM
Last Post: stevendaprano
  a module can be found but another cannot Skaperen 7 2,759 Oct-25-2021, 12:16 AM
Last Post: Skaperen
  Eric6 No module named 'serial' BvdP4Py 0 2,735 Jul-30-2021, 08:38 AM
Last Post: BvdP4Py
Lightbulb Feedback needed! Controlled entropy in programs with 'ordered' module grandrew 0 1,440 Jul-27-2021, 01:49 AM
Last Post: grandrew
  PIL module and Python 2.7 under Windows 10 Lad 3 2,218 Jul-06-2021, 07:39 PM
Last Post: Lad
  finding a mismatched triple-quote in a huge module Skaperen 1 1,623 Apr-15-2020, 04:29 AM
Last Post: UGuntupalli
  random module lacks instantiation Skaperen 3 2,340 Dec-03-2019, 02:53 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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