Where are the standard python modules located? I'm talking about the random, turtle, etc. modules.
Asked
Active
Viewed 199 times
4 Answers
0
See for yourself:
>>> import random
>>> random
<module 'random' from '/usr/local/lib/python3.6/random.py'>
>>>
The default __repr__
implementation for modules will print out their fully qualified system path.
The precise location depends on what platform your working on and what installation method you used.

Brian61354270
- 8,690
- 4
- 21
- 43
0
These are located under the Lib
sub directory in Main Python installation directory.
for example: if you have installed Python
in C:\Program Files\Python37
.
The standard packages will be in C:\Program Files\Python37\Lib
.

Saurabh Kansal
- 643
- 5
- 13
0
Python looks for modules in the listed directories in sys.path
Just like if you defined your own module in a path that isn't recognized by Python where you have to add it in the environment variable PYTHONPATH to have it included in the directories listed in sys.path and then be able to import it
~$ # let's see contents of sys.path
~$ python3
Python 3.8.2 (default, Mar 13 2020, 10:14:16)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages']
>>> exit()
~$
~$ # now, let's try to see contents of /usr/lib/python3.8
~$ cd /usr/lib/python3.8
python3.8$
python3.8$ # list and grep the contents of some files
python3.8$ ls | grep random
random.py
python3.8$ cat random.py | grep "class Random"
class Random(_random.Random):
python3.8$ ls | grep collections
collections
_collections_abc.py
python3.8$ ls collections/
abc.py __init__.py __pycache__
python3.8$ cat collections/__init__.py | grep "def namedtuple"
def namedtuple(typename, field_names, *, rename=False, defaults=None, module=None):
python3.8$ cat collections/__init__.py | grep "class OrderedDict"
class OrderedDict(dict):
python3.8$ cat collections/__init__.py | grep "class Counter"
class Counter(dict):

Niel Godfrey Pablo Ponciano
- 9,822
- 1
- 17
- 30