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?
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 thesys.argv
variable accessible via thesys
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 filescript.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!
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.