How Python works

How Python works



module          module


statement       statement       statement        statement


expression      expression       expression       expression      expression       expression


object            object          object           object         object           object         object           object


It’s true for CPython that’s the standard version we use with the Python name. It’s based on ANSI C.

Source————————->Byte Code—————————>Runtime                                            plik.pyc                                       Python Virtual Machine

writing                                            compilation                               interpretation

dependent on you                 automatic, independent            dependent on the system we use

Python looks for a plik.pyc, and then if the first one isn’t present.


Source—————————–>Similar to Java


Source—————————–>Similar to .NET

To catch the output in a file:

python > plik.txt

To catch the input in a file:

python < plik.txt

Usually, we can run the file in Windows:
But when we have not set the PATH environment variable, we must provide the full path to our Python interpreter:

On new Windows, however, we can write (if our PATH is set):

We can provide the full path to the folder with our files (if our PATH isn’t set):
python D:\python33\scripts\
If our PATH isn’t set for Python’s directory, Python, and our file is in other folder than we are working in, we use:

D:\Python33\python D:\python33\scripts\

There are and plik.pyw. The latter will display only a window made by our script, not DOS console window as usual. Such a file has a special behavior on Windows. It’s used for user interfaces.

We can import a Python module only once per session (process). We can use import many times — no changed will be. But we can reload the module.
>>> from imp import reload
>>> reload(plik)

A module is a package of variable names that is known namespace. These name in this package are know attributes (an attribute is a variable name that is attached to any object).

We have the following module:


And we can do:

Get the whole module:
>>> import plik
We like Polish. We love Polish.
>>> plik.b, plik.c
(‘We like Polish.’, ‘We love Polish.’)

Copy the names:
>>> from plik import a, b
>>> b, c
(‘We like Polish.’, ‘We love Polish.’)

We can see the values in parentheses because they are tuples. dir can get all names in a module:
>>> dir(plik)
[‘__builtins__’, ‘__doc__’, ‘__file__’, ‘__name__’, ‘__package__’, ‘a’, ‘b’]

A program is composed of many modules linked together by import statements. Each  module is a self-contained package of variables that is a namespace. One module doesn’t see the names in another file. However, we can import them

Remember — import is better than from!

When Python imports, it started to seek in sys.path – a Python list of directory name strings in the sys module, which is initialized form PYTHONPATH, and set of standard directories.

We can run the code in our module using:


We don’t need import and reload our module. exec runs the current version of the file without reloading.
>>> exec(open(‘’).read())
We like Polish.
We love Polish.
>>> exec(open(‘’).read())
We like Polish.
We love Polish.
We worship Polish.

exec runs the module anew. It can overwrite variable we are using (like from). It’s better not to use that.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s