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, ...)
arg1, arg2, ... should be used within
How to execute the Python file within your Python code by passing the arguments to the Python file?
Solution: The following four steps will help you run a Python
script.py with arguments in your Python code.
- Define a Python file
script.pythat accesses the arguments using the
sys.argvvariable accessible via the
sysmodule. 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.argvwith your desired values in the calling Python script.
- Load the Python file
script.pyinto 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:
# 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']
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!
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.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.