Deploying Python applications¶
Pynsist is a tool that bundles Python programs together with the Python-interpreter into a single installer based on NSIS. In most cases, packaging only requires the user to choose a version of the Python-interpreter and declare the dependencies of the program. The tool downloads the specified Python-interpreter for Windows and packages it with all the dependencies in a single Windows-executable installer.
The installed program can be started from a shortcut that the installer adds to the start-menu. It uses a Python interpreter installed within its application directory, independent of any other Python installation on the computer.
A big advantage of Pynsist is that the Windows packages can be built on Linux. There are several examples for different kinds of programs (console, GUI) in the documentation . The tool is released under the MIT-licence.
Application bundles¶
Windows¶
py2exe¶
py2exe is a distutils extension which allows to build standalone Windows executable programs (32-bit and 64-bit) from Python scripts. Python versions included in the official development cycle are supported (refers to Status of Python branches). py2exe can build console executables and windows (GUI) executables. Building windows services, and DLL/EXE COM servers might work but it is not actively supported. The distutils extension is released under the MIT-licence and Mozilla Public License 2.0.
macOS¶
py2app¶
py2app is a Python setuptools command which will allow you to make standalone macOS application bundles and plugins from Python scripts. Note that py2app MUST be used on macOS to build applications, it cannot create Mac applications on other platforms. py2app is released under the MIT-license.
Unix (including Linux and macOS)¶
pex¶
pex is a library for generating .pex (Python EXecutable) files which are executable Python environments in the spirit of virtualenvs. pex is an expansion upon the ideas outlined in PEP 441 and makes the deployment of Python applications as simple as cp. pex files may even include multiple platform-specific Python distributions, meaning that a single pex file can be portable across Linux and macOS. pex is released under the Apache License 2.0.
Configuration management¶
FIXME puppet salt chef ansible fabric
How do I deploy a Python desktop application?
I have started on a personal python application that runs on the desktop. I am using wxPython as a GUI toolkit. Should there be a demand for this type of application, I would possibly like to commercialize it. I have no knowledge of deploying «real-life» Python applications, though I have used py2exe in the past with varied success. How would I obfuscate the code? Can I somehow deploy only the bytecode? An ideal solution would not jeopardize my intellectual property (source code), would not require a direct installation of Python (though I’m sure it will need to have some embedded interpreter), and would be cross-platform (Windows, Mac, and Linux). Does anyone know of any tools or resources in this area? Thanks.
5 Answers 5
You can distribute the compiled Python bytecode (.pyc files) instead of the source. You can’t prevent decompilation in Python (or any other language, really). You could use an obfuscator like pyobfuscate to make it more annoying for competitors to decipher your decompiled source.
As Alex Martelli says in this thread, if you want to keep your code a secret, you shouldn’t run it on other people’s machines.
IIRC, the last time I used cx_Freeze it created a DLL for Windows that removed the necessity for a native Python installation. This is at least worth checking out.
Maybe IronPython can provide something for you? I bet those .exe/.dll-files can be pretty locked down. Not sure how such features work on mono, thus no idea how this works on Linux/OS X.
Wow, there are a lot of questions in there:
- It is possible to run the bytecode (.pyc) file directly from the Python interpreter, but I haven’t seen any bytecode obfuscation tools available.
- I’m not aware of any «all in one» deployment solution, but:
- For Windows you could use NSIS(http://nsis.sourceforge.net/Main_Page). The problem here is that while OSX/*nix comes with python, Windows doesn’t. If you’re not willing to build a binary with py2exe, I’m not sure what the licensing issues would be surrounding distribution of the Python runtime environment (not to mention the technical ones).
- You could package up the OS X distribution using the «bundle» format, and *NIX has it’s own conventions for installing software— typically a «make install» script.
Python’s bytecode is very high level and trivially decompileable; distributing .pyc files is really not any form of protection you want to rely on.
How to Deploy Python Flask Application with Apache on a Windows Server
It took me about 3 days to figure out how to deploy a flask application on a windows server using Apache so I thought I should write a tutorial to help someone else and for my own reference in the feature. Please let me know if you have any questions because I know this is not an easy task to understand. If you have any questions feel free to ask.
- Install python for all users. Note install it for all users.
- Set python as the environmental variable like so
3. Open the comandline and Run pip install virtualenv like so
4. Create a directory called myapp
C:\myapp\Flask\Scripts>pip install flask
Now you have installed your flask infrastructure
We are going to create a file in our root directory called index.py using any text editor like notpad or notepad++
9. Past the content below into it
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello from FastCGI via IIS!" if __name__ == "__main__": app.run()
10. Now go to the command prompt and run
i. (flask) C:\myapp\Flask\Scripts>cd ..
iii. And Run (flask) C:\myapp\app>python index.py
12. Now open your browser and type in http://localhost:5000
We have now setup flask to run on the development server.
Now we have to configure apache to run python and serve it to the world.
14. Extract the files and copy Apache24 to C:\Apache24
15. Navigate to C:\Apache24\bin and run or click on httpd.exe to start apache
16. Go to your browser and type in http://localhost
We have now setup a development server for apache.
Now lets configure the system to run python from apache. But before we start, go here and download mod_wsgi. Without it you cannot run python flask on windows.
Note: if you are using 64bit system download 64bit Apache,64bit python and use the 64bit mod_wsgi.so and if you are using a 32bit system use everything that is 32bit.
17. Look for the right mod_wsgi.so file and copy it to C:\Apache24\modules and past it.
18. Navigate to C:\Apache24\conf and open httpd.conf in a text editor
19.Add LoadModule wsgi_module modules/mod_wsgi.so to the modules
20. Remove the # from LoadModule cgi_module modules/mod_cgi.so
21. Add +ExecCGI to your Options it should look like this
Options Indexes FollowSymLinks +ExecCGI AllowOverride None Require all granted 22. Locate the tag and inside it, add
AddHandler cgi-script .cgi AddHandler cgi-script .py
23. Locate this tag and inside it add index.py and it will look like this
DirectoryIndex index.php index.php3 index.html index.htm index.cgi index.py 24. Now create another file called index.py inside the root directory here C:\myapp>
#!C:\Python34\python.exe import cgitb cgitb.enable() print("Content-Type: text/html;charset=utf-8") print() print("Hello World!")
27. Go to your browser and type http://localhost/index.py. It will say Hello World meaning you are now running python scripts on apache server
28. Now find out how to use virtual host in apache if you have no idea. Add the code below to the virtual host file
ServerName localhost WSGIScriptAlias / "C:/myapp/app/web.wsgi" DocumentRoot C:/myapp/app Options +Indexes +Includes +FollowSymLinks +MultiViews +ExecCGI AllowOverride All Require local 29. Create a file called web.wsgi in the root of your application like so
import sys sys.path.append('C:/myapp/app/') from app import app as application
30. Now create another file in the root of your application called app.py and add the following code.
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello from FastCGI via IIS!" if __name__ == "__main__": app.run()
32. Navigate to http://localhost and you will see this
Useful links
If you enjoyed this article, share or tell a friend and be on the lookout for more from storehubs.com/Blog