Esempio n. 1
0
import sys
import importer

module1 = importer.import_('module1', 'module1_source.py', '.')

print('sys say:', sys.modules.get('module1', 'module1 not found'))

import module2
module2.hello()
Esempio n. 2
0
import sys
import importer

module1 = importer.import_('module1', 'module1_src.py', '.')
print(f"sys says: {sys.modules.get('module1', 'module1 not found')}")

import module2
module2.hello()
Esempio n. 3
0
import os
import textwrap
from termcolor import colored

from world import World
# from player import Player
from content import rooms
from content import creatures
from utils import print_error

# Test importer module
import importer

Player = importer.import_('player', 'player.py', '.').Player

#
# Main
#

# Make a new player object that is currently in the 'outside' room.

# Write a loop that:
#
# * Prints the current room name
# * Prints the current description (the textwrap module might be useful here).
# * Waits for user input and decides what to do.
#
# If the user enters a cardinal direction, attempt to move to the room there.
# Print an error message if the movement isn't allowed.
#
# If the user enters "q", quit the game.
Esempio n. 4
0
import sys
import importer

module1 = importer.import_(
    'module1', 'module1_source.py',
    'Part 1\\Section 09 - Modules, Packages and Namespaces\\example3b\\')

print('sys says:', sys.modules.get('module1', 'module1 not found'))

import module2

module2.hello()
Esempio n. 5
0
# main.py
import sys
import os.path

# we import our custom importer module
import importer

# import module1.py using our own importer
module1 = importer.import_('module1', 
    os.path.join(os.path.dirname(__file__), 'module1_source.py'), '.')

# we can see that module1 is in sys.modules
print('sys says:', sys.modules.get('module1', 'module1 not found'))

# and we can now import this module "normally" from other locations
# such as module2.py
import module2
module2.hello()

# notice how the first time we imported (using import_) module1, it "ran" (printed running module1).
# but the second time we imported it (in module2) it did not
# that's because Python recovered module1 from cache, and did not rebuild it
Esempio n. 6
0
import sys
import importer #it will print Running importer.py

module1 = importer.import_('module1','module1_source.py','.') #it will import modules from module1_source.py in the same directory(cuz '.')

print(globals()) #this will contain module1 in the globals() dictiionary
module1.hello()

import module2
module2.hello() #if we run this hello function it won't print importing module1.py as module1 is imported only once and then it is registered in sys.modules hence when module1.hello is called in module2, it won't import it again and then just run the hello function in module1
Esempio n. 7
0
import sys
# The importer execs the code
import importer

# Need a reference
# Create a module1
print(f"{'=='*40}")
print(f"{'*'*20} calling the import_ {'*'*20}")
module1 = importer.import_('module1', "module1_src.py", '.')
print(f"{'=='*40}")

# The importer has sets up where the module is from
print(f"sys says: {sys.modules.get('module1', 'module1 not found')}")
module1.hello()

print(f"\n\n{'*'*20} calling import for module2 {'*'*20}")
import module2
# If a module is already imported it does not rerun the code
module2.hello()

# The hard part is where the code comes from the src file.. anywhere.. a zip.. builtin..
# But the process is the same
print(
    "\n\n\t1) Checks the sys.modules cache to see if the module has been imported"
)
print("\t2) if not it creates a new module object (types.ModuleType")
print("\t3) loads the src code")
print("\t4) adds an entry to sys.modules")
print("\t5) compiles and executes the src code")
Esempio n. 8
0
##### This part does what import does

module_rel_path = os.path.join(module_path,module_file)
module_abs_path = os.path.abspath(module_rel_path)

# Read the file
with open(module_rel_path,'r') as src_file:
    src_code = src_file.read()
#Create a module object
m           = types.ModuleType(module_name)
m.__file__  = module_abs_path

#set up a reference in sys.modules
sys.modules[module_name] = m

#compile the source code

obj_code = compile(src_code,filename = module_abs_path,mode='exec')

# assign the namespace of the 
exec(obj_code,m.__dict__)
'''

####### Import is done

m = importer.import_("module1", "module1.py", ".")
m.print_dict('main.globals', globals())

print("\n\n--------------------------------------------------\n")