Shell integration with setuptools - python

I'd like to make a Python application available for the current Shell/Desktop Environment (e.g. Gnome, Windows, etc.).
Specifically I want to modify file associations and/or programs listed in a file's context menu ('open with..'). And I want to have my program 'registered' as available application (which has different meanings on Linux desktop environments and Windows)
Can I do things like this with setuptools? Can I for example make my own picture viewer be startable via the gnome shell and be associated with image files in file managers after installing it with pip?

You can create a command line tool using either scripts or entry_points keywords in your setup function of a setup.py file. (Personally I prefer to use scripts as it is more concise and in addition allows to run a package binary executables if you need)
see the example and more detailed explanations here:
http://python-packaging.readthedocs.io/en/latest/command-line-scripts.html
Further, what i usually do for my PyQt application is the following. After the package installation on Ubuntu I run the app as my script alias from the shell. Then, I can lock it to the launcher and use the link for the fast access. You can also mention the path to your 'executable' script location in your "open with..." preferences.
I know that it might be not the full behavior you need but still hope it can help you.

Related

How to add command line tool to python package

I have written a simple package called fcards that is currently on PyPI. The package provides a curses based interface for creating and practicing flash cards, and right now, in order to run it, the user must type "python -m fcards.fcards". I would like to know how to make it so the user can simply type "fcards" to run the application.
I've tried using the "scripts" and "entry_points" arguments in the setup function in my setup.py file but so far nothing has worked consistently on all platforms.
Usually, I write a console driver and put it in a file console.py as the main function.
Then in setup.py:
entry_points={
'console_scripts': ['<name> = <name>.console:main'],
},
where <name> is the name of the module. This has worked out-of-the-box on UNIX-like platforms. It also worked using the Anaconda Python distribution on ms-windows.
On ms-windows the distinction between console_scripts and gui_scripts is important, because the latter have to be associated with another python binary. This is done by giving them the extension pyw instead of py. UNIX-like platforms (incuding macOS, I guess) generally don't care.

Bundling and distributing nautilus-python extensions with application

I've written a python application and I use cx_freeze to freeze the scripts and create the executable. Then I'm making it into a single executable bin package using shell scripts.
Recently I developed a context menu extension using nautilus-python and would like to include the same with my application bundle. Obviously I can't place the .py file under under ~/.local/share/nautilus-python/extensions. I tried with just placing the .pyc file alone with executable bit enabled for the script which didn't work.
Any pointers reg this would be greatly helpful.
cx_Freeze is fine for Windows or Mac but on Linux systems, applications are expected to be installed with package manager. Usually, distributions have designated people who will create the packages. Alternatively, you can choose distributions you want to support and create the packages for them yourself, or you can use service like OBS.
If you really want to provide a single executable for people to drop into a directory they have on PATH, you will need to provide the extension separately.
Please do not make the application install the extension on start-up, user should retain control over their computer. Or if you do that, please add a setup.py flag, so that distributions can easily disable it.

How to distribute a stand-alone python application?

I want to distribute my Python application to co-workers for them to use. The application will on be run on Linux systems, but the users do not have admin privileges so cannot install my application's module dependencies. I would the users to be able to untar my application and then run my main.py script. Running another one-time 'install'-type script is okay, but not much else.
PyInstaller is close to what I want. Except I would like to distribute the source code of my application as well. So the application should be stand-alone and self-contained (with or without the python interpreter is fine, preferably with), but users should be able to make small changes to the code and rerun the application. My ideal solution is to create some sort of compressed/compiled archive of all my applications module dependencies and distribute that with my application. It doesn't have to be all dependencies, but at least the non-standard packages. The application will then import modules from this archive instead of the user's PYTHONPATH.
I tried virtualenv, but having the users source the activate script was a little too much. I've been looking into numerous other solutions, but I can't find one that works for me.
Why don't you create a directory with the interpreter you want to use, add in any modules etc. Then drop in a bash script, say run.sh which calls the program. It can launch your chosen interpretter with your python files, arguments etc.
Any source files can remain this way and be edited in place. You could tar and distribute the whole directory, or put in something like git.
One approach is to use virtualenv. It is designed to create isolated python environment and does a good job at it. It should be possible (link to package the virtualenv with your app with some effort. However virtualenv is not designed for that so it's not as easy as it could be.
package-virtualenv GitHub project might also help.

How can I make portable python desktop application?

The requirement is to make an application portable, meaning no installer. I looked at py2exe and I am afraid I need to run install if I want to run it under Windows.
So my question is, can I make a portable python desktop application without any installation (all dependencies and libs are packaged), dragging from USB / CD will run it?
(This is critical because it's a headache for users to install C++ Run Time library...)
Thanks.
You can use this method with py2exe: http://www.py2exe.org/index.cgi/SingleFileExecutable
Basically, you use NSIS to package all of the required files and folders into a single executable. When you run it, the required files are expanded to a temporary directory, the executable is run, and when it exits, the temporary files are deleted automatically.
There is also an example that comes with py2exe which uses Inno Setup instead of NSIS to achieve the same result. It's installed to site-packages\py2exe\samples\extending.
You can also fork Portable Python and modify to include your application and libraries you need. It runs from any drive/network location without installation and you can pick do you want 2.x.x or 3.x.x based Python core

How to deploy Python to Windows users?

I'm soon to launch a beta app and this have the option to create custom integration scripts on Python.
The app will target Mac OS X and Windows, and my problem is with Windows where Python normally is not present.
My actual aproach is silently run the Python 2.6 install. However I face the problem that is not activated by default and the path is not set when use the command line options. And I fear that if Python is installed before and I upgrade to a new version this could break something else...
So, I wonder how this can be done cleanly. Is it OK if I copy the whole Python 2.6 directory, and put it in a sub-directory of my app and install everything there? Or with virtualenv is posible run diferents versions of Python (if Python is already installed in the machine?).
I also play before embedding Python with a DLL, and found it easy but I lost the ability to debug, so I switch to command-line plug-ins.
I execute the plug-ins from command line and read the STDOUT and STDERR output. The app is made with Delphi/Lazarus. I install others modules like JSON and RPC clients, Win32com, ORM, etc. I create the installer with bitrock.
UPDATE: The end-users are small business owners, and the Python scripts are made by developers. I want to avoid any additional step in the deployment, so I want a fully integrated setup.
Copy a Portable Python folder out of your installer, into the same folder as your Delphi/Lazarus app. Set all paths appropriately for that.
You might try using py2exe. It creates a .exe file with Python already included!
Integrate the python interpreter into your Delphi app with P4D. These components actually work, and in both directions too (Delphi classes exposed to Python as binary extensions, and Python interpreter inside Delphi). I also saw a patch for Lazarus compatibility on the Google Code "issues" page, but it seems there might be some unresolved issues there.
I think there's no problem combining .EXE packaging with a tool like PyInstaller or py2exe and Python-written plugins. The created .EXE can easily detect where it's installed and the code inside can then simply import files from some pre-determined plugin directory. Don't forget that once you package a Python script into an executable, it also packages the Python interpreter inside, so there you have it - a full Python environment customized with your own code.

Categories

Resources