1

quick question here about if this is possible or not. For a command line script I'm writing, I want the user to be able to enter a .py file as an argument, and I would like the script to import the .py file and seek out a specific variable contained inside.

My problem is that python expects an import statement to have the actual filename as the variable, which in my case I can't do (since I don't know the name of the .py file before the user inputs it):

    elif training_file.suffix == '.py': # we expect partially annotated text here
        import training_file

This doesn't work, and if I try to open it like I would a regular file, like so...

    elif training_file.suffix == '.py': # we expect partially annotated text here
        with open(training_file, 'r') as training_file:
            for line in training_file:
              ...

... it just reads the raw text (as would be expected), and doesn't register python formatting. Is there a way to import and interact with a python file without knowing the name of that python file before starting the program? Is there a different way of doing this?

Thanks so much for any tips or guidance.

0 Answers0