Tag: Modules & imports

  • importlib.reload: Reimporting a Python module during development

    Once you import a Python module, a second import won’t reload it. That’s usually good. But what if you want to? (Common in development and debuggin.) This forces the reload.

    Read more

  • Python import: How a module name becomes a file on sys.path

    You import a Python module with the variable you’ll define, not a filename. “import mymod” tells Python to find mymod.py in each dir in sys.path. The first directory is ” (the empty string) — i.e., the directory where the program is running (i.e., not where it was defined).

    Read more

  • Globbing and Python’s “subprocess” module

    Python’s “subprocess” module makes it really easy to invoke an external program and grab its output. For example, you can say import subprocess print(subprocess.check_output(‘ls’)) and the output is then $ ./blog.py b’blog.py\nblog.py~\ndictslice.py\ndictslice.py~\nhexnums.txt\nnums.txt\npeanut-butter.jpg\nregexp\nshowfile.py\nsieve.py\ntest.py\ntestintern.py\n’ subprocess.check_output returns a bytestring with the filenames on my desktop. To deal with them in a more serious way, and to have the…

    Read more