I am learning python from "Learn Python The Hard Way", I came across a source code

"from sys import argv" and got an explanation that sys is a module from where argv is imported.

Although, sounds simple but still not able to understand the concept behind. I would like to know how they picked up the module sys or how we gonna decide from where we need to import.

please help!

Recommended Answers

All 4 Replies

I'm really sorry, I don't know Python. But I just felt compelled to ask ... why choose to learn Python the hard way? What's wrong with the easy way?

The import statement is the mechanism that allows python programs to use libraries. Using libraries is a key feature in any programming language. It allows our programs to benefit from all the work that has been done before by others.

Suppose you're a clever mathematician and you write a python function to factorize integers, then you will write your own module named for example clever_mod and this module will contain a function factorize(). If you publish your module on pypi for example, anybody can download it an use your function by writing

from clever_mod import factorize
result = factorize(9237982379866234)

As of today, there are 97775 python modules in pypi, and an unknown number of available modules that are not on pypi.

sys is a standard library module in python, which means that it is one of the few dozens modules that automatically come with every python install. sys.argv is the list that contains the arguments that your script received from the command line when its execution was launched.

@Dani "Learn python the hard way" is a well known python tutorial. The idea is to get your hands dirty by actually typing every character of the tutorial's programs and examples in order to learn python faster. It sounds like a reasonable idea.

Beside Gribouillis' explanation.

Although, sounds simple but still not able to understand the concept behind.

Core concepts are the most hard things.

I would like to know how they picked up the module sys or how we gonna decide from where we need to import.

The author picked it, because the desired object - the argv variable - is in this module. Period, end of discussion.

Why this functionality is in this module is because the author of python interpreter - beeing a built in module - decided to put it into this module.

How an author of a module decides about a module name, and the functionality of the module is up to his judgement.

However, module names are considered good, if a user (a programmer) can safely guess the functionality of a module by its name, and can find a module name by knowing the functionality he expects.

Based on my exprience on programming I would look for "command line argument", if I did not know python:

  • In builtins or globals, as in C or perl
  • in some operationg system specific module, because its os specific
  • in some system specific module, because it is global to a program
  • in something called shell, because shell is the first to process it

Anyway a lasy programmer usually types into the google: Command line arguments python
And gets the answer...

Just to answer in simple terms, you are right saying sys is a module. How come then you know that? Thats what you've asked, I think. Let me say this: From every module we import, there are methods. In each module there are functions, that operate on data that is passed as arguments. Period! Now comes the need for you to read thoroughly the documentation for the particular language at hand, in your case python.

argv is used to pass values, for instance, argv[0] is the first value passed, and so on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.