If you’re looking for a clean way to run a Python file from another Python function, `subprocess` is the way to go.
Here’s a quick example:
```python
import subprocess
subprocess.run(["python", "your_script.py"])
```
But if you want to avoid spawning a new process, you can use `importlib` to import the file as a module and call its functions directly.
```python
import importlib.util
spec = importlib.util.spec_from_file_location("module_name", "path/to/your_script.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
module.main()
```
Both methods have their pros and cons, so choose based on your needs!
Here’s a quick example:
```python
import subprocess
subprocess.run(["python", "your_script.py"])
```
But if you want to avoid spawning a new process, you can use `importlib` to import the file as a module and call its functions directly.
```python
import importlib.util
spec = importlib.util.spec_from_file_location("module_name", "path/to/your_script.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
module.main()
```
Both methods have their pros and cons, so choose based on your needs!
