Reputation: 13
I am studying python, both 3. and 2. I started a few days ago. I want to know the differences between site module and interpreter. I got this question from
Python exit commands - why so many and when should each be used?
Those explanations are very clear but it's still hard to me.
Upvotes: 0
Views: 110
Reputation: 550
I assume you are stuck on understanding:
"Nevertheless, quit should not be used in production code. This is because it only works if the site module is loaded. Instead, this function should only be used in the interpreter."
Basically, what that is saying is that quit
is a part of a module loaded in the python interpreter. That module's name, is site.
Firstly, the python interpreter is what you use to run python scripts or environments. It interprets python commands. For example, if you write a = 1
in a script or python environment, the interpreter takes that command and executes it without compiling it. (If it was a language like c you would need to compile the script before you run it).
Secondly, a module is a pre-written file that can define functions, classes and variables. When you write import numpy
into python, you are importing the module, numpy. Therefore when they say "this only works if the site module is loaded", that means that import site
must be executed.
When you start a python interpreter (by typing python
into your command shell), it automatically imports site
, which has sys
, venv
and main
etc. which are all required to run an active interpreter session.
Upvotes: 0
Reputation: 254
If I am understanding your question correctly,site
is a module in Python. A module is a file containing Python definitions and statements. In order to use the functions (for ex: exit()
or quit()
, you need to import the site
module as those respective functions are defined in there.
The Python interpreter
is the program that reads and executes Python code. This includes source code, pre-compiled code, scripts - in this case you reference, you would need to import the site
module into your current Python interpreter
session, in order to use say exit()
or quit()
in that given session.
So, the process of this particular question would be:
* Activate the Python interpreter by typing into your respective terminal the version of Python you have installed on your computer, ex. python3
.
* In the Python interpreter, type import site
Hope that helps Hwan.
Upvotes: 1