0

I'm trying to make a compilation of all my python projects so I can document my progress as I continue to learn. In my quest to learn more, I tried learning some basic I/O. I've structured my library to have an index program, start_here.py at the top of my directory, a subdirectory named "projects", and 2 files dice_app.py and text_to_math.py in that subdirectory.

I'm trying to structure this so the user manually activates start_here.py, then it opens a readme file with instructions on how to use my library. I can successfully read the readme and project list txt files. The struggle I'm having right now is executing either the dice app or text to math from start_here.

This is my current system (does not work):

f = open("readme_files/index.txt")
p = open("readme_files/projects.txt")

print(f.read())

func = 0
while True:
    func = int(input("Requested Operation: "))

    if func == 0:
        print(p.read())
    elif func == 1:
        exec("projects/dice_app.py")
        break
    elif func == 2:
        exec("projects/text_to_math.py")
        break
    else:
        print("Invalid operation. Please try again.")

I've quintuple-checked that I have the correct relative paths and there are no misspellings for any file names. I've tried import statements but when I did that it automatically executed text_to_math, the second file in the projects subdirectory.

I'm not trying to execute a specific function from the file, but run the entire thing start to finish. What can I do to get this to work?

P.S. I am very new to python so I don't know the "right way" to organize files but this is how my directory is set up. my file structure

2 Answers 2

1

Use

elif func == 1:
    import projects.dice_app

Or

import subprocess

...

elif func == 1:
    subprocess.run('projects/dice_app.py')

If you put the import statement in the elif then you won't have problems with modules getting automatically executed. Or subprocess.run is how you run terminal commands, exec is used for something else.

Sign up to request clarification or add additional context in comments.

3 Comments

That worked! Thanks for the help. If that's not what exec() is used for, then how is it used? PS: When I used the import strategy to get back to start_here.py from one of the projects, it only worked once getting back to the start_here once. Is there a way to make it so I can go back and forth multiple times?
You give exec the code you want it to execute as a string such as exec("a=2+2"). The variable a will now contain the value 4. Eval works similar. With eval you do a = eval("2+2"). It's kind of a hackish way of getting things done, and it's usage is sometimes frowned upon unless you really need it.
In webapps it is dangerous to use exec (and eval) with data from the user. If you ask the user for a number, and they give you a Python function, and you use exec on that, you've now created a vulnerability in your code as anyone interacting with your webapp can now get your webapp to run any arbitrary Python code that they want. This doesn't really matter for command line applications like yours though, as the user already has the ability to run Python code.
1

Say you have the following project structure:

project_name/
    __init__.py
    main.py
    utils.py

In main.py, you could then say:

from utils.py import myfunc

You however cannot import from main to utils and from utils to main at the same time. This would cause an ImportError.

To match your filestructure:

root_dir/
    text_to_math/
        __init__.py
        dice_app.py
        text_to_math.py
    __init__.py
    start_here.py

Then in start_here.py:

from text_to_math.dice_app import myfunc

2 Comments

I've fixed my post to have the details of my file structure. That's not how I have my workspace organized
@Coderman Edited solution to fit your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.