Locating Your Installation
When first installing Python, depending on what you select in the installation, there are a couple of places Python could be installed (you could also select your own directory). Luckily this can be easily found by executing the following in Python:
import sys print(sys.executable)
This will print the location of python.exe which is the executable that runs Python scripts. Common places for Python to be installed on is in
To run Python scripts, you need to pass a filename as an argument to python.exe so python.exe can run it. For example, if you wanted to execute myscript.py, you would call
python myscript.py which calls
python.exe (regarding it can be found on the PATH) and passes the filename "myscript.py" as an argument.
Where you located python.exe is also the root of your Python installation (assuming you aren't using a virtual environment). In this folder you will find files relating directly to Python and modules you have installed. Under the headings below, I'm going to explain what is in each of the folders in the root folder and notable subdirectories in them.
python.exe vs pythonw.exe
Before we start looking at the folders, you should notice there is a
python.exe and a
pythonw.exe file. These do very similar jobs but have one primary difference,
python.exe will open a terminal window whereas
pythonw.exe will not.
.py files are associated with
.pyw files are associated with
pythonw.exe. This means if you want to see the output from your script (like
.py, otherwise you will want to use
.pyw to hide the terminal window that appears.
Here is a writeup of the smaller differences between them.
DLLs/ contains dll files relating to Python. When looking in here, you may notice that there are actually
.pyd files in here.
.pyd files are actually the same as dll files but they include a function
PyInit_MODULE() where "MODULE" is the name of the dll file (without the extension). You can read more about this in the docs.
include/ contains header files for the Python/C API
Lib/ contains the Python standard library; that is, all the modules that come with Python like
re and many others.
A very notable folder in this folder is
site-packages. This is where Python modules are installed to when you install them using
pip or a
setup.py file. Looking in this folder for a module you want to investigate can allow you to see the source.
If you import a Python module in a Python script and then print the module, the path output will typically be in this folder; for example:
import flask print(flask)
For me outputs
<module 'flask' from 'C:\\Python36\\lib\\site-packages\\flask\\__init__.py'>. C:/Python36 is where Python is installed for me and you can see flask is sitting in lib/site-packages.
libs/ contains native code libraries in comparison to
Lib/. This means whereas most files in
Lib/ are human readable,
libs/ typically contains compiled libraries.
Here is a bit more information on this folder.
Scripts/ contains executables and other "scripts" that can be run. Typically this is where modules will put executables so they are then located in the terminal/OS calls using the PATH variables.
Putting executables in here allows us to easily execute a command which will be looked for in here (ragarding this folder is in the PATH variable) and then executed. If you set console scripts in a
setup.py file, the names you specify will be created as executables in here.
Here is an example of a console script that I mentioned. This allows you to call
auto-py-to-exeon the command line to start the application.
tcl/ contains tkinter source files as tkinter runs on tcl.
When packaging Python to an executable, you might find some files from here in the result folder/executable; some times it can be safe to remove these to make the package smaller in size.
Tools/ contains Python tools supplied in the distribution. Looking in this folder surprised me when I first saw them as they are practically examples and demos of the Python language doing something useful.
Here are some examples of these:
scripts/md5sum.py: "Python utility to print MD5 checksums of argument files"
scripts/diff.py: Finds the difference of two files. "Command line interface to difflib.py providing diffs" in different formats.
scripts/google.py: Open a search term in a new browser window/tab