How to Execute a Python File with Arguments in Python?

5/5 - (1 vote)

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

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

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

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

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 with arguments in your Python code.

  • Define a Python file 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 into a Python string.
  • Pass the Python string into Python’s built-in exec() function. This runs the code defined in the file

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:

import sys

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("")
code =

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

The line sys.argv = ['Alice', 'Bob', 'Carl'] sets the arguments that are read by your file 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!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. 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?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming 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.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!