H
H
HuleGun2016-05-20 22:45:41
Python
HuleGun, 2016-05-20 22:45:41

What is the order in which Python modules are loaded?

Such a situation: there are 2 files in the current directory - sys.py and test.py. Why is it that when I import sys in test.py , it imports the standard library and not my file, since the list of module search paths (sys.path) lists the current directory first?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dima Cherny, 2016-05-21
@DemosBlack

The sys.path variable contains a list of strings with the names of the directories in which to search for modules. It is initialized from the value of the PYTHONPATH environment variable and the default built-in value. You can add a path:
import sys
sys.path.append(/home/my/lib/python)
You can use the built-in dir() function to find out the names defined in a module. It returns a sorted list of strings:
dir(sys)
Each module has its own namespace, which is the global scope for all the functions it defines. In order for the variables of this module not to conflict with other global names or other modules, you must use the prefix: _module_name_._variable_name_ .

A
ADRian, 2016-05-21
@ADR

Если вы хотите подменить sys только для своем программы то можно как-то так:
import my_sys as sys

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question