Non-root Installation of Python Modules

104 27

    Modules

    • Modules allow a Python program to access and execute code that is not actually typed out within it source code file. These can include other classes or code that the programmer has written himself, or functions that are part of the Python standard library or specialized libraries that other Python developers have released for general use. By typing "import (module name)" at the top of a Python source code file, the Python interpreter will read in the contents of that module. This allows the programmer to reference the code inside the module as if he had typed it out within the source code file.

    $PATH

    • When the Python programmer imports a module, the Python interpreter will first look for the Python file inside the current directory, then look through a series of other directories that are defined in the system's "$PATH" environmental variable or the more specialized "PYTHONPATH" variable. If the interpreter cannot find the module in one of these directories, it will produce an error an not execute the Python program. To avoid having to physically place the module files in the same directory as the current project's source code, programmers generally store module files they will be using frequently in a special folder that Python appends to these environmental variables when it installs on the system.

    Root Installation

    • This folder for Python modules is usually located within the Python interpreter's directory, which only users with root permissions can modify. If a programmer who has root privileges wants to install a new module to this directory, it is as simple as using the command line with "su" or "sudo" to directly copy module files into the directory or executing the modules install script with these permissions. However, if a programmer does not have root privileges, he will not be able to install the module to the protected central directory.

    Non-Root Installation

    • Programmers with non-root privileges who want to use a particular module have two options. They can manually copy the module file into their project directory whenever they want to use it, or they can run a modified version of the module install script. Append "--user" to the end of the "python setup.py install" terminal command. This will cause the script to install these files to a directory within the user's home directory, which the user would have the necessary permissions to modify. This directory is part of the list that the Python interpreter will look through for module files and will allow the user, but not other users on the system, to import these modules as normal.

Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.