Why does python not import every module at startup automatically?
module, python, python-2.7, python-import
Solution
Because you don't need all of it. There is no point in loading every library if you don't need them.
EDIT: I copied my libs folder to a test directory and made it into a package by adding an `__init__.py` file to it. In this file I added:
import os
import glob
__all__ = [ os.path.basename(f)[:-3] for f in glob.glob(os.path.dirname(__file__)+"/*.py")]
I created a test script that contains:
from Lib import *
print('Hello')
When I try to run it in the shell all it does is print 'The Zen of Python' by Tim Peters, opens this webcomic in my browser (2 things I absolutely did not see coming) and throws the following error:
Traceback (most recent call last):
File "C:\Users\Hannah\Documents\dropBox\Python\test\test.py", line 1, in <module>
from Lib import *
AttributeError: 'module' object has no attribute 'crypt'
It takes a noticable amount of time before it does any of this, about 10-15 seconds
Problem
I was having a play around with Python 2.7 and everybody knows that at the start of every program, you always have to import modules. For example: ``` import random import time for x in range(1, 300): print random.randint(1,100) time.sleep(1) print "Done!" ``` Anyway, I was thinking, why do I have to import all my modules manually? Why doesn't Python just import them all like this. Sure, I can understand why it does not import like this: ``` from random import randint from time import * for x in range(1, 300): print randint(1,100) sleep(1) print "Done!" ``` As some function names may clash. But, if you have to define where the function is at the start, so for example `random.` in `random.randint(1,100)`. Now modern computers are so powerful, it seems logical to import every module automatically instead of wasting lines of code, and time by having to find which module you need then importing it manually when it can easily be automated. So, why does python not import every module at startup automatically? EDIT: I have made a new version of a little program that imports every module that I can find by running: ``` import sys sys.builtin_module_names ``` Here are the results: ``` x = int(1000000) def test(): global x x -= 1 print "Iterations Left: ", x import __builtin__ import __main__ import _ast import _bisect import _codecs import _codecs_cn import _codecs_hk import _codecs_iso2022 import _codecs_jp import _codecs_kr import _codecs_tw import _collections import _csv import _functools import _heapq import _hotshot import _io import _json import _locale import _lsprof import _md5 import _multibytecodec import _random import _sha import _sha256 import _sha512 import _sre import _struct import _subprocess import _symtable import _warnings import _weakref import _winreg import array import audioop import binascii import cPickle import cStringIO import cmath import datetime import errno import exceptions import future_builtins import gc import imageop import imp import itertools import marshal import math import mmap import msvcrt import nt import operator import parser import signal import strop import sys import thread import time import xxsubtype import zipimport import zlib def start(): from timeit import Timer t = Timer("test()", "from __main__ import test") print t.timeit() start() ```