How To distribute Python solutions

Clarification

There are many ways to distribute Python solutions, and this describes one method. For more information on alternatives, jump to the end of this article.

Assumptions

[DEV] and [PROD] are used to represent the development and production environments. The code is transferred from the [DEV] to the [PROD] environment.

The solution will be run when no one is logged into the computer (via Windows Task Scheduler). Also, the [PROD] environment is a server environment where certain restrictions may apply. The account of the installer may differ from the account running the code.

A requirements.txt file was created on the [DEV] side. If you are not familiar with this file and its purpose, visit https://www.freecodecamp.org/news/python-requirementstxt-explained/. The command is:

pip freeze > requirements.txt

During development, unused packages can accumulate in requirements.txt. This can occur, for instance, when evaluating similar packages. These redundant packages inflate the solution's footprint and increase the risk of distribution errors. Uninstall unused packages before distributing the solution.


  1. [DEV] Take note of the Python version your solution is running. With Python, it is possible to have multiple versions installed on a single computer, and you want to use the version associated with your solution. A major version change could break packages that you invoke in your code.

To get the Python version, from your development virtual environment type:

python --version

  1. [PROD] Download and install that version of Python from https://www.python.org. On python.org’s front page, you are shown the latest version; if you need an earlier version, click on View the full list of downloads.

Minor version differences should not break your code.

  1. Install Python. The settings chosen here allow the code to be run from any account without the need for a particular user account to be logged in.  1
  • Use admin privileges when installing py.exe was checked.
  • Add python.exe to PATH was checked.
  • Customize installation was selected.
  • py launcher was checked
  • for all users (requires admin privileges was checked
  • All options that allow any user to invoke Python are checked
  • The install directory is the default Windows Program Files directory
  1. [PROD] Verify that Python has been installed

python --version

  1. [PROD] Transfer the solution to a folder that will house it.

If the solution is on a local network, a simple copy and paste could suffice. Alternatively, for wider distributions or more automation, you could use git to synchronize repositories. The [DEV] environment would update the repo, and [PROD] environments would source the updated code from the same. This topic is not discussed here.

The following source folders should not be transferred: 

  • Any folder that the IDE creates (e.g., .vscode for VS Code) 
  • __pycache__
  • .venv

  1. [PROD] In the solution folder, create its virtual environment:
python -m venv .venv
  1. [PROD] Activate the virtual environment and install the libraries the solution requires:
.\.venv\Scripts\activate && pip install -r requirements.txt

Verify that the packages have installed without errors. Some packages rely on external programs and may throw errors if base libraries are missing. These errors typically explain how to install the underlying solution.

  1. [PROD] If asked to upgrade pip, do so with the provided command.

Running the Solution

  1. To run the solution, the virtual environment needs to be activated first. The following batch file called run_py.cmd automates the process:
REM the solution is in the folder c:\<solution folder>
cd c:\<solution folder>
CALL c:\<solution folder>\.venv\Scripts\activate.bat
python %1

To run your code, simply call run_py.cmd "<name of your python code>".

Automating with Windows Task Scheduler

  1. If your solution will be run directly, this section does not apply to you. For automated background tasks (e.g., checking emails periodically), Task Scheduler can automate the process without requiring login.

The screenshots show a task run without user login, invoking the run_py.cmd file with the Python script name as an argument.

Thank you

I would like to thank the community at https://www.reddit.com/r/learnpython, some of whom enthusiastically shared valid input that has been incorporated into this HowTo. One particularly useful and insightful reply is Reddit post.


 

Follow This, That and (Maybe), the Other:

 

Comments

Popular posts from this blog

20150628 Giarratana Circular

HOWTO setup OpenVPN server and client configuration files using EasyRSA

How to clone and synchronise a GitHub repository on Android