How can I run a Python file from another Python function effectively?

22 Replies, 1459 Views

Wow, thanks everyone for the awesome tips! I tried using `subprocess` like a few of you suggested, and it worked perfectly.

I also experimented with `importlib`, and it’s super cool for running a Python file from another Python function without spawning a new process.

One quick follow-up—if I want to pass arguments to the script using `subprocess`, do I just add them to the list like this?

```python
subprocess.run(["python", "your_script.py", "arg1", "arg2"])
```

Also, is there a way to handle errors if the script fails?

Thanks again, you guys are the best!

Messages In This Thread



Users browsing this thread: 1 Guest(s)