Python 3 Programming Tutorial – Sys Module

Home/Programming Video Tutorials/Python Video Tutorials/Python 3 Programming Tutorial – Sys Module

Python 3 Programming Tutorial – Sys Module

FavoriteLoadingAdd to favorites

This video covers some of the basics to the sys module in Python 3. The sys module allows you to use stdin() and stdout(), as well as stderr()… but, most interestingly, we can utilize sys.argv(). To many this is a confusing concept, but it is pretty simple and very useful once you learn it. The idea of sys.argv is to allow you to pass arguments through to Python from the command line.

This ability acts as a bridge to the ability to communicate between Python and other languages.

Sample code for this basics series: http://pythonprogramming.net/beginner-python-programming-tutorials/

Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M

http://seaofbtc.com
http://sentdex.com
http://hkinsley.com

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6

By | 2017-06-14T02:21:15+00:00 June 14th, 2017|Python Video Tutorials|50 Comments

50 Comments

  1. Weasel Words June 14, 2017 at 2:22 am - Reply

    Great video, Thanks! It shows me how to pass parameters or values to a python script and then manipulate those values within the script. But how do you return a value or values to the calling program, your example is just printing to console.

  2. Andy Handy June 14, 2017 at 2:22 am - Reply

    First of all, big thanks. You really helped me out starting from zero, learning to program in these tutorials.
    But in this lesson I have some trouble.
    I think it has to do with my python installation on a mac:
    Mac has python 2.7 installed by default.
    I have installed Python 3.4.

    The terminal will not open the sys.py file
    (My guess is that it is doing that because it reads .py in python 2.7 by default).

    I can open the file however with the Python Launcher which is the right python 3.4 edition,
    but it logs out and says ‘proces completed’ after which I can’t type in anything.

    I can open python3 in the terminal by typing python3 in the beginning but still, opening this file is not working so far.
    Thanks again, Andy

  3. Vardhaman Srivastava June 14, 2017 at 2:23 am - Reply

    Traceback (most recent call last):
    File "ex13.py", line 3, in <module>
    script, first, second, third = argv
    ValueError: not enough values to unpack (expected 4, got 1)

    Pls help it is showing this error!

    from sys import argv

    script, first, second, third = argv

    print("The script is called:", script)
    print("Your first variable is:", first)
    print("Your second variable is:", second)
    print("Your third variable is:", third)

    this is my test code.

  4. Sheldon June 14, 2017 at 2:24 am - Reply

    i didn’t understand anything too much information

  5. wine cheese June 14, 2017 at 2:26 am - Reply

    I want to make a program that changes the brightness of my computer with python3.
    How can i use this module i dont get it.
    Could you guys help me out?

  6. Jakob Jensen June 14, 2017 at 2:27 am - Reply

    Hi great tutorials!

    Im pretty new to python and i think this one was difficult to understand, so could you maybe do one more about sys and maybe show how to make python and php talk together using sys? 

  7. Brandon Kowalski June 14, 2017 at 2:28 am - Reply

    In the first one, when I run the .py without a preceding "python … .py" the argv gives a longer result, namely that whole path.. Can you explain some of this?

    C:UserskowalAppDataLocalProgramsPythonPython35>example_sys.py
    this is stderr text
    this is stdout text
    [‘C:\Users\kowal\AppData\Local\Programs\Python\Python35\example_sys.py’]

    C:UserskowalAppDataLocalProgramsPythonPython35> python example_sys.py
    this is stderr text
    this is stdout text
    [‘example_sys.py’]

  8. Aditya Kuppili June 14, 2017 at 2:29 am - Reply

    What’s flush?

  9. Ahmed Ahmed June 14, 2017 at 2:30 am - Reply

    Do Getopt plz!

  10. 94mathdude June 14, 2017 at 2:34 am - Reply

    You named your file sys.py. Same name as what you are importing. Why wasn’t there a error running the program?

  11. Faris Amirmudin June 14, 2017 at 2:35 am - Reply

    what is the function of sys.stderr.flush()

  12. Tarunbir Singh June 14, 2017 at 2:35 am - Reply

    why do you write ‘python sys.py’ in the command line…. why not just ‘sys.py’ ???

  13. Raju Mondal June 14, 2017 at 2:36 am - Reply

    can u shoe some of your php code where u used python sys module? so it will b better for me to understand … thnks in advanse..i am an old subscriber of yours

  14. Chris Dimitrov June 14, 2017 at 2:39 am - Reply

    Found it very useful, thanks!

  15. The Molynator June 14, 2017 at 2:40 am - Reply

    hey im a little confused here.

    1.  so stdin, stdout are just used for printing input/output text as in prompts? and stderr is for red error text?  Im unclear on what other uses these have.

    2. having trouble with argv.  i think naming the file sys.py is a little confusing.  I take it you had two identical files ( lets name them temp1.py and misc2.py. ) in two different folders (one at c:/code and other c:/code/testdir) with these lines in them:

    import sys
    print(sys.argv)

    So if im right the main idea is youre able to pass values (arguments) back and forth between one or two python scripts via the command line?  sort of like printing or modifying file contents through CLI?

    also I tried typing python misc2.py in cli and it said cant find file (in either folder). so somethings up with my path.  i standard installed python and checked the add py to path option.  feels like i need to add another line to env variables, googled this issue and couldnt get a clear answer.  thanks for help, sorry if this is long im meticulous and just got hung up on this lesson

  16. Priyank Jain June 14, 2017 at 2:42 am - Reply

    Hey Harrison,I wanted to ask why do you use traditional python idle over pycharm?
    Is it good to use pycharm as a beginner?

  17. Mohamed Gabr June 14, 2017 at 2:43 am - Reply

    got it …sorry….we added 5 to the argument 🙂

  18. Matthias Attong June 14, 2017 at 2:43 am - Reply

    can you pass arguments into a python file with HTML or Java script

  19. Joseph Trapani June 14, 2017 at 2:45 am - Reply

    +sentdex Thanks for all you are doing, Im following your tutorials as a complete nube, and so far its been amazing!

    If I may, I have a small problem understanding something about this tutorial;

    entering python sis.py (just doesnt work)
    entering sis.py (gives off the complete directory adress)
    and entering py -3 sys.py (gives the file name i.e. sys.py)

    I found the py -3 online and am running python version 3.51 but so is a friend of mine and your example works fine for him. Would really appreciate a clarification on this! but can understand if you don`t have the time.

    Either way, keep up the good work and thanks!
    😀

  20. Itay elgard June 14, 2017 at 2:45 am - Reply

    to make a cmd bigger write ‘mode 300,300’

  21. Nanashi Hitori June 14, 2017 at 2:46 am - Reply

    Could you explain how to make a program that have arguments. like Linux commands "ls -l" or "ls -a" or multiple args. I have some ideas with if…elif statements regarding arguments but i stop when i try to write for compound arguments.

  22. Colby Cox (@JordanColby00) June 14, 2017 at 2:48 am - Reply

    Since you have helped me so much, I figured it was just to thank you for all of your work! I have used a lot of different tutorial sets, but none of them have went into the depth of including useful modules like you have. I appreciate all your efforts. 

  23. Kazutadashi June 14, 2017 at 2:51 am - Reply

    How would you allow the user to have multiple argv inputs? instead of forcing argv[1]

  24. Leo Ferrari June 14, 2017 at 2:52 am - Reply

    Can someone help me out please?

    When I tried typing in the cmd window the following message arose:

    "’python’ is not recognised as an internal or external command, executable program or batch file"

    Back when I installed Python 3.5.2 I did not explicitly install pip. Is that where I’m going wrong? When I watched one of the early tutorials on pip he said that pip would be included with the Python program, so we could reference python in the cmd window straight away.

    Thanks!

  25. Itay elgard June 14, 2017 at 2:55 am - Reply

    wouldn’t you use something like argparse to get value into a file? it hes way more options and just cleaner i think

  26. Bill Barnes June 14, 2017 at 2:55 am - Reply

    Can you give me an example where you used python and php together? How did you get the python to fire from php? I’m new.

  27. Anthony Vipond June 14, 2017 at 2:55 am - Reply

    Can you do a tutorial on ZeroRPC?

  28. q zorn June 14, 2017 at 2:56 am - Reply

    Mmm, smart guy.

  29. Mohamed Gabr June 14, 2017 at 2:57 am - Reply

    I don’t understand…. how we typed 5 and it printed 10 ????? ….sorry but really don’t understand this passing arguments….thanks

  30. Jomann June 14, 2017 at 2:57 am - Reply

    This was really useful to me! but I feel like beginners would be a bit confused, especially when you started mixing up and restating things later in the video. so you may want to redo this video at a later time so its easier to understand.

  31. Jurriaan Buitenweg June 14, 2017 at 2:59 am - Reply

    Yo amazing videos, learning alot from them but maybe for your next series its maybe fun to go into cyber security so maybe doing like SSH clients or maybe even a python debugger, but keep it up! subscribed 😀

  32. Cyril Attié June 14, 2017 at 3:00 am - Reply

    Hi! Once more, thank you for all your videos Harrison!

    I am new in programming and don’t see very well what could be a argument given to the command line. I understand that I can write (literally, like in the video) several arguments after the directory> , but when the computer is running a Python program, what are the arguments passed to the command line?

    (I am trying to see the useful side of the sys module, but I don’t really get the usefulness of the command line, the black window)

  33. Ivankovic Luka June 14, 2017 at 3:00 am - Reply

    Does this "sys.argv" combine some files together to make a program and then you have a program which is made of 2 files, does it ?

  34. Mikael June 14, 2017 at 3:01 am - Reply

    Snowden, is that you? 😀

  35. eathenbad June 14, 2017 at 3:04 am - Reply

    Other videos were fine but this one DOES NOT EXPLAIN anything. Python is the first language I’m learning and dude, you’re explaining ANYTHING and I don’t understand a thing in this one.

  36. Ivankovic Luka June 14, 2017 at 3:04 am - Reply

    I don’t understand actually why we use [1] at the end of the sys.argv? Can you please explain.

  37. Yahya Haluk June 14, 2017 at 3:04 am - Reply

    Hey, can you explain how to do RPN with sys.argv if possible?

  38. Tore He June 14, 2017 at 3:05 am - Reply

    I just wanted to say thanks! thease python turorial have helped me so much! keep ut the great work! Python is my first language and i can actually make som small simple programs thanks to you!! 

  39. Alpaca Male June 14, 2017 at 3:05 am - Reply

    dude how can I convert sys.argv to utf-8 in order to get asian characters and other strange characters like ñ or ô etc…??? I’ve looked all over google and nothing

  40. Harsh Dobariya June 14, 2017 at 3:05 am - Reply

    Can this library give the information about connected USB devices?

  41. Sadika Verma June 14, 2017 at 3:06 am - Reply

    hello sir i m using python 2.7 n when i want to execute sys.py from python cmdline then it returns error file stdin invalid syntax

  42. dheeraj bajaj June 14, 2017 at 3:07 am - Reply

    Very well explained … I loved the Tutorial
    Can  we have some more sessions on Website data filtering via urllib2 and Socket Programming via Python ?

  43. blacksailorx blacksailorx June 14, 2017 at 3:08 am - Reply

    You have no pedagogical skills,you just jump from an idea to another!

  44. Elliot Hulse's Breath June 14, 2017 at 3:11 am - Reply

    how can I give a URL as parameter to a program?

  45. teja kaipa June 14, 2017 at 3:13 am - Reply

    I loved all your tutoriials but ……. this complete tutorial was unclear … can u be more clear . especially with examples and please do take care about having window CLEARLY …. its abit messy

  46. johnny quest June 14, 2017 at 3:17 am - Reply

    Being on a MAC, how would you recommend opening that "sys" folder into the Terminal? I can’t seem to find a way to do that.

  47. Delia Drumm June 14, 2017 at 3:18 am - Reply

    I’m working with a PoSH script right now and I’m trying to pass the information from that script, into my script in python. What I’m having a hard time trying to figure out is this:

    def get_websockets_enabled(customer_id):
    p = subprocess.Popen([‘powershell.exe’, ‘C:usersheythereThis_isMyFile.ps1’], stdout=sys.stdout)

    But I’m getting stuck because I’m not sure if this is actually getting the output of this file and how I am supposed to pass this into my python script.

  48. pegp June 14, 2017 at 3:18 am - Reply

    I have a small problem, I’m running python 3.5; when I write the code
    "
    import sys

    sys.stderr.write(‘This is stderr textn’)
    sys.stderr.flush()
    sys.stdout.write(‘This is stdout textn’)
    "

    the output is
    "
    This is stdout text
    This is stderr text
    "

    The output text is first while the error output is last although I put the error output first. even when i put a test text after the error out it still gets printed before the error out can anyone explain the reason for it?

  49. rohit poddaturi June 14, 2017 at 3:19 am - Reply

    Could you do a tutorial on Docx module ??

  50. Naf R June 14, 2017 at 3:20 am - Reply

    Out of all the previous videos I have seen so far of this tutorial, this is by far has the poorest explanation. Please make another video clearly explaining these concepts

Leave A Comment

*