-2

I just started programming and I am learning Python using A Byte of Python. I am having a problem with the modules chapter. I don't really understand modules very well, so maybe this is a dumb question, but I copied and pasted the input and it returned a different output. The input that he suggests is

import sys

print('The command line arguments are:')
for i in sys.argv:
    print i

print '\n\nThe PYTHONPATH is', sys.path, '\n' 

and this is the output:

$ python module_using_sys.py we are arguments
The command line arguments are:
module_using_sys.py
we
are
arguments


The PYTHONPATH is ['/tmp/py',
# many entries here, not shown here
'/Library/Python/2.7/site-packages',
'/usr/local/lib/python2.7/site-packages']

However my output using the same input is different. Here it is:

pedro@pedro-Inspiron-3521:~/Desktop/python$ python modulo1.py
The command line arguments are:
modulo1.py

The PYTHONPATH is ['/home/pedro/Desktop/python', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

I understand that when you write import sys you are making the built in module that as things related to the system available for use. I also know that the for looping statement iterates over a sequence, and the sequence in this case should be the command line arguments that exist. However, it just appears modulo1.py, which is the name of the file where i saved the code i copied and pasted. I don't really understand the pythonpath thing, but according to my research it is the list of directories where potentially exist modules. I use gedit as text editor and ubuntu as os and ubuntu's terminal. What did I do wrong?

Lukas Graf
  • 30,317
  • 8
  • 77
  • 92
chilliefiber
  • 571
  • 2
  • 7
  • 18
  • 2
    It's impossible to answer your question without proper indentation. Can you fix that for us? Use four spaces to signify a code block – Adam Smith Jul 24 '14 at 18:59
  • 1
    You don't pass any command line args when you run `modulo1.py` – Padraic Cunningham Jul 24 '14 at 19:01
  • 1
    I've attempting to format your code. Please take a look and ensure indention is correct – Andy Jul 24 '14 at 19:02
  • 2
    OP: I don't understand what is wrong. You didn't seem to run `modulo1.py` with any command line arguments, so why would any show up? More to the point: what did you expect your output to be? – Adam Smith Jul 24 '14 at 19:03
  • You are also running the code in two different places, where did you run the first .py from? – Padraic Cunningham Jul 24 '14 at 19:07
  • @PadraicCunningham I believe the first code block is the example code from OP's tutorial – Adam Smith Jul 24 '14 at 19:08
  • Ignore the PYTHONPATH for now - one version of the script is being run on Mac OS X, the other on Linux - the contents of PYTHONPATH will be vastly different. Instead note that the name of the Python module (`module_using_sys.py` vs `modulo1.py`) is different, and so are the arguments: `we are arguments` vs. no arguments. – Lukas Graf Jul 24 '14 at 19:09
  • 1
    @AdamSmith, then I am totally lost as the code is doing exactly what it should then. – Padraic Cunningham Jul 24 '14 at 19:09
  • Ok, the indentation is correct. Sorry for being a bad wanna be programmer, but I don't know why it appears we are arguments in the book's output. Again, this is likely a very stupid thing to ask, but I don't understand what happened. – chilliefiber Jul 24 '14 at 19:10
  • To make it clearer... the book is running `$ python module_using_sys.py we are arguments` but you are only running `$ python module_using_sys.py`. – nofinator Jul 24 '14 at 19:11
  • Also, note that `$ python module_using_sys.py we are arguments` is part of the **input**, specifically the command line the script is being run with. So you need to run yours the same way if you expect the same output. – Lukas Graf Jul 24 '14 at 19:11
  • So how do I make it the same? – chilliefiber Jul 24 '14 at 19:14
  • @pedromatias Try this: `pedro@pedro-Inspiron-3521:~/Desktop/python$ python modulo1.py we are arguments` – nofinator Jul 24 '14 at 19:16

1 Answers1

2

The first statement.

sys.argv is the list of command line arguments passed to a Python script (https://docs.python.org/2/library/sys.html)

When you run $ python modulo1.py, modulo1.py is your first (and only) argument. That's why the output is modulo1.py. But in your book, it runs $python module_using_sys.py we are arguments, here python is the commend to invoke python. module_using_sys.py is the first argument passed to your computer, we is the 2nd, are is the 3rd, and arguments is the last argument.

What the for loop do is to print each of these arguments.

So if you would like to see exactly the same output as in your book, you should:

  • rename your .py file to the same name as in your book
  • pass the same arguments to your console

The second statement.

Note that the value of your sys.path is different from the value of sys.path in the book that you are referring to. This is why you see different output.

Your understanding about PYTHONPATH is generally correct. For more information about PYTHONPATH, you can check this answer: How to use PYTHONPATH or this official page

Community
  • 1
  • 1
JoYSword
  • 432
  • 1
  • 3
  • 14