0

I currently have a GUI made with tkinter that allows the user to select radio buttons that will import different python scripts that all perform different functions. Some of these scripts are in the same directory, so importing has not been an issue, but I have a few scripts in a different directory (has to stay there due to dependencies), but want the user to be able to select this script from the GUI to have it run instead of them having to go into terminal and run the script that way.

More details: My current file is in Finance/Ingest while I have a script, combine.py, in dw/

What I want: I have radio button and when the user click the submit button it calls this function, which gets the radio buttons values. If the value == 1 then I want it to import the script and run it. Below is whaty I currently have but it keeps saying "cannot find module dw." Any help would be appreciated!!!

def Rel_Button():
     Run_DB_Rel_Parse = DB_Rel_Parse.get()
     Run_DB_Req_Rel = DB_Req_Rel.get()
     Ingest_DB_Rel = DB_Rel.get()

     if Run_DB_Rel_Parse == 1:
          import dw.combine

1 Answers1

2

Python looks for modules along the python path. There's a variety of ways you can set it.

  • Set PYTHONPATH environment variable to point at directory containing dw
  • Add dw to a python package that you install via pip/setuptools
  • Set sys.path in your program to point at the directory containing dw

The last solution is not ideal but it's typically easiest to implement. Let's assume that your module is in ../../dependencies/library. Since sys.path is a modifiable list, you could do something like so:

import sys
from os.path import abspath, dirname, join
sys.path.insert(0, abspath(join(dirname(__file__), '..', '..', 'dependencies', 'library')))

Note that this assumes that the dw module is a proper python package. It either should be dw.py or a directory called dw that has a file called __init__.py (it can be empty) in it.

However, if you're going to use this as something someone else is going to run, you're better off packaging your modules and installing them all via pip.

Dustin Spicuzza
  • 694
  • 6
  • 12