Advertisements

A platform independent way to set your Python Path for your Python applications

In a software development house where desktop computers run Microsoft Windows while servers run Linux, software developers will have to ensure that the Python code that they wrote on their Windows machine can run on the deployment servers which are running Linux.

One unavoidable task for Python application developers is the importing of functionalities that are contained in other Python scripts. In order for the Python interpreter to find the Python scripts that are referenced by Python import statements, the Python Path will need to contain the URLs of the directories that contain the Python scripts to be imported.

This post documents how I set my Python Path for my Python applications in a platform independent way.

The platform dependent way to set your Python Path

One platform dependent way that you may configure your Python Path is by setting the PYTHONPATH environment variable through a script file targeted at the native command-line interpreter that is provided by the Operating System where your Python application will run on.

To run your Python application on Windows, you may create a batch file, run.bat with the following contents:

set PYTHONPATH=%PYTHONPATH%;C:\your_python_lib

python app.py

And to run your Python application on Linux, you may create a shell script, run.sh with the following contents:

export PYTHONPATH=$PYTHONPATH:/your_python_lib

python app.py

This approach of configuring Python Path for your Python applications will result in duplicated effort whenever you import new library scripts that are located in new directories.

The platform independent way to set your Python Path

Setting your Python Path in a platform independent way will avoid duplicated effort whenever you need to import new library scripts that are located in new directories. I typically follow the following guidelines to set my Python Path in a platform independent way:

  • Place the library scripts in a directory that is near to the starter script.
  • Set Python Path inside the starter script.

Placing the library scripts in a directory that is near to the starter script

I tend to follow the following directory structure whenever I need to create new Python applications:

.
|--app.py
|--import
   |--techcoil
      |--__init__.py
      |--controller
         |--__init__.py
         |--users.py
         !--products.py
      |--storage
         |--__init__.py
         |--mongo.py
         |--file.py
      |--service
         |--__init__.py
         |--users.py
         |--products.py

In the above directory structure, I designate app.py as the starter script. I then designate a directory , which I named as import, in the same directory as app.py. I will place the module packages that my Python application need to import inside the import directory.

Udemy offer

Setting Python Path inside the starter script

To set the Python Path inside the starter script, I will include the following Python codes at the top:

import os
import sys
root_directory = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(root_directory, 'import'))

### Application specific logic ###

In the above codes, I first import the os and sys in-built Python modules. I then proceed to get the directory path where the executing script resides in (in this case, app.py) and set the value to the root_directory variable.

I then construct the url to the import directory and append it to the sys.path variable. This will make the modules inside the import directory visible to the Python interpreter.

Advertisements

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people.

Udemy deals
Advertisements