3

File setup:

...\Project_Folder
...\Project_Folder\Project.py
...\Project_folder\Script\TestScript.py

I'm attempting to have Project.py import modules from the folder Script based on user input.

Python Version: 3.4.2

Ideally, the script would look something like

q = str(input("Input: "))
from Script import q 

However, python does not recognize q as a variable when using import.

I've tried using importlib, however I cannot figure out how to import from the Script folder mentioned above.

import importlib
q = str(input("Input: "))
module = importlib.import_module(q, package=None)

I'm not certain where I would implement the file path.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
  • 1
    Dynamically importing scripts is a relatively advanced thing that you rarely want to do - there is probably a better way (loading data, rather than code). If you really do need to do this (e.g: for plugins, say), you will need the make the folder a Python package (add an `__init__.py` file) or on the Python Path (add to `sys.path`). – Gareth Latty Mar 03 '15 at 16:26
  • I'll elaborate on the goal of this program. The whole premise is to be able to have a master program, Project.py, that will be able to execute commands stored in a separate folder, scripts folder, in the form of .py files. The whole purpose is to allow Project.py to be able to perform a variety of task, without needing additional changes to Project.py. For example, if I want Project.py to be able to open Google Chrome, I can create a .py file inside the scripts folder titled Google. Then typing Google into the input field for Project.py will run Google.py without a change in Project.py – Adam Coster Mar 03 '15 at 17:17
  • What would be a better way of doing this type of modular construction? – Adam Coster Mar 03 '15 at 17:18
  • Do you want to *import* those files or *run* them? Because those are 2, slightly, different things. – Bakuriu Mar 03 '15 at 17:22
  • Perhaps my example was poorly chosen. There will be information in the imported files that I want Project.py to be able to use. Simply running those files, for example using os.startfile, will not work. – Adam Coster Mar 03 '15 at 17:30
  • I'm attempting to use Lattyware's solution, by adding a '__int__.py' file to the Script directory, however this doesn't seem to be working. I followed the instructions given here. http://effbot.org/pyfaq/what-is-init-py-used-for.htm – Adam Coster Mar 03 '15 at 17:43

2 Answers2

0

Repeat of my answer originally posted at How to import a module given the full path? as this is a Python 3.4 specific question:

This area of Python 3.4 seems to be extremely tortuous to understand, mainly because the documentation doesn't give good examples! This was my attempt using non-deprecated modules. It will import a module given the path to the .py file. I'm using it to load "plugins" at runtime.

def import_module_from_file(full_path_to_module):
    """
    Import a module given the full path/filename of the .py file

    Python 3.4

    """

    module = None

    try:

        # Get module name and path from full path
        module_dir, module_file = os.path.split(full_path_to_module)
        module_name, module_ext = os.path.splitext(module_file)

        # Get module "spec" from filename
        spec = importlib.util.spec_from_file_location(module_name,full_path_to_module)

        module = spec.loader.load_module()

    except Exception as ec:
        # Simple error printing
        # Insert "sophisticated" stuff here
        print(ec)

    finally:
        return module

# load module dynamically
path = "<enter your path here>"
module = import_module_from_file(path)

# Now use the module
# e.g. module.myFunction()
Community
  • 1
  • 1
Redlegjed
  • 187
  • 2
  • 11
0

I did this by defining the entire import line as a string, formatting the string with q and then using the exec command:

    imp = 'from Script import %s' %q
    exec imp