How to Execute a Python File with Arguments in Python?

Problem: You have a Python code file script.py and you want to run it in your Python shell with arguments.

Example: You seek a function execute("script.py", arg1, arg2, ...).

# Some Python Code
# ...
execute("script.py", arg1, arg2, ...)

The arguments arg1, arg2, ... should be used within script.py.

How to execute the Python file within your Python code by passing the arguments to the Python file?

How to Execute a Python File with Arguments in Python?
Visual Explanation

Solution: The following four steps will help you run a Python script.py with arguments in your Python code.

  • Define a Python file script.py that accesses the arguments using the sys.argv variable accessible via the sys module. This variable is defined on operating system level, so you can pass it within your calling Python script and fill it with the desired arguments.
  • Fill the variable sys.argv with your desired values in the calling Python script.
  • Load the Python file script.py into a Python string.
  • Pass the Python string into Python’s built-in exec() function. This runs the code defined in the file script.py.

In order to pass arguments to your Python script, you will need to import the sys module. Once this module is imported in your code, upon execution sys.argv will exist, containing a list of all of the arguments passed to your script.

Consider the following script:

# script.py
import sys
print(sys.argv)

To test the waters, let’s run this script from a command prompt with python3 Alice Bob Carl:

$ python3 Alice Bob Carl
['Alice', 'Bob', 'Carl']

Here’s how you can run this code in your local Python script and define the arguments as well:

# your local script
import sys

script = open("script.py")
code = script.read()

# set the arguments to be read by script.py
sys.argv = ['Alice', 'Bob', 'Carl']

The line sys.argv = ['Alice', 'Bob', 'Carl'] sets the arguments that are read by your file script.py using the same sys.argv variable. Again, note that the variable sys.argv is defined on a global operating system level. So, any change will be seen by any script that accesses this variable.

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!