High schooler
High schooler

Reputation: 1680

Dynamic Module Python

I have a program that creates a module called "cool" using file operations. I later say import cool and then uses the cool module that was created.

Here is my directory

project/
    main.py
    modules/
        maker.py
        cool/          #this folder and its contents was made by maker.py
            __init__.py 
            coolm.py

If I want to make my program into the .exe format, this strategy will not work anymore. Does anyone know another technique?

Note: I cannot use exec to use the cool module..

Upvotes: 0

Views: 108

Answers (1)

Aesthete
Aesthete

Reputation: 18848

Import your module when you need it like this:

coolmod = __import__('coolm')
coolm.someproperty

Alternatively you could try:

import importlib
coolmod = importlib.import_module('coolm', 'cool')

This allows you to specify the package name as a second argument.

Upvotes: 1

Related Questions