Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Package a python app like spyder does

I have an open source python software (GridCal) that has a GUI made with PyQt5. The program is pip-installable pip3 install GridCal.

I would like to know what do I have to do so that when someone pip-installs my program, it appears on the system menus like when one installs Spyder (The python IDE)

So far, all I can provide is the setup.py of my program, but I don't know if it is relevant.

from distutils.core import setup
import sys
import os

name = "GridCal"

# Python 2.4 or later needed
if sys.version_info < (3, 5, 0, 'final', 0):
    raise (SystemExit, 'Python 3.5 or later is required!')

# Build a list of all project modules
packages = []
for dirname, dirnames, filenames in os.walk(name):
        if '__init__.py' in filenames:
            packages.append(dirname.replace('/', '.'))

package_dir = {name: name}

# Data_files (e.g. doc) needs (directory, files-in-this-directory) tuples
data_files = []
for dirname, dirnames, filenames in os.walk('doc'):
        fileslist = []
        for filename in filenames:
            fullname = os.path.join(dirname, filename)
            fileslist.append(fullname)
        data_files.append(('share/' + name + '/' + dirname, fileslist))

setup(
    # Application name:
    name=name,

    # Version number (initial):
    version="1.025",

    # Application author details:
    author="Santiago Peñate Vera",
    author_email="[email protected]",

    # Packages
    packages=packages,

    data_files=data_files,

    # Include additional files into the package
    include_package_data=True,

    # Details
    url="http://pypi.python.org/pypi/GridCal/",

    # License file
    license="LICENSE.txt",

    # description
    description="Research Oriented electrical simulation software.",

    # long_description=open("README.txt").read(),

    # Dependent packages (distributions)
    install_requires=["numpy",
                      "scipy",
                      "networkx",
                      "pandas",
                      "xlwt",
                      "xlrd",
                      "PyQt5",
                      "matplotlib",
                      "qtconsole"
                      ],
)
like image 755
Santi Peñate-Vera Avatar asked Jan 31 '17 14:01

Santi Peñate-Vera


People also ask

What packages does Spyder come with?

Spyder integrates with a number of prominent packages in the scientific Python stack, including NumPy, SciPy, Matplotlib, pandas, IPython, SymPy and Cython, as well as other open-source software.

What is a Spyder in Python?

Spyder is a cross-platform, open-source ide (IDE) for scientific Python programming. Spyder works with a variety of popular Python packages, like NumPy, Matplotlib, pandas, SymPy, and Cython, and other open-source applications.

Is Spyder the best Python IDE?

The best Python IDEs for Windows are PyCharm, Spyder, Pydev, IDLE, Wing, Eric Python, etc. Python is a popular programming language which was developed in 1991.


1 Answers

You can find how spyder-ide has implemented this functionality in their setup.py script at line 77, here's the code:

def get_data_files():
    """Return data_files in a platform dependent manner"""
    if sys.platform.startswith('linux'):
        if PY3:
            data_files = [('share/applications', ['scripts/spyder3.desktop']),
                      ('share/pixmaps', ['img_src/spyder3.png'])]
        else:
            data_files = [('share/applications', ['scripts/spyder.desktop']),
                      ('share/pixmaps', ['img_src/spyder.png'])]
    elif os.name == 'nt':
        data_files = [('scripts', ['img_src/spyder.ico',
                               'img_src/spyder_reset.ico'])]
    else:
        data_files = []
    return data_files

then it gets called in the regular data_files setting of the setup() instance.

What it does is instruct the installer to copy the required files to make a menu item in their respective folders.[0]

In linux systems you need to create a appName.desktop file, these files usually reside in /usr/share/applications or /usr/local/share/applications for applications installed system-wide, or ~/.local/share/applications for user-specific applications[1].

File structure looks like this (self-explanatory, a reference to more keys can be found in the reference link above):

[Desktop Entry]
Version=1.0
Terminal=false
Type=Application
Name=AppName
Exec=/path/to/executable
Icon=/path/to/icon
Categories=Graphics;2DGraphics;Development;
Comment=Tooltip comment appears when you hover on menu icon.

You can choose to place your app icon anywhere and use its full path, or simply place under share/pixmaps/ and reference it just by its filename.

Notice that the script doesn't use full paths when specifying the locations for the .desktop and icon files to be installed at, this is because the locations will be relative to the current sys.exec_prefix which defaults to /usr/local/[2]

On windows you need to create a .lnk (binary file) in the ~\Windows\Start Menu\Programs directory, i wont be able to explain in details how it works (not a windows programmer), for that they wrote a complete post-installation script that deals with making menu shortcuts for windows[3].

A little clarification for the windows post-installation script:
It seems like the mechanism that implements its execution has been removed from spyder-ide's setup.py file, so all you people digging to find how it works, you won't be able to find out unless you check the file history, so to clear the confusion, here's what you can do to get it to execute (not the spyder-ide way):

  • Create a custom command PostInstallCommand and instruct setup() to run it by adding it to the cmdclass attribute like so cmdclass={'install': PostInstallCommand} check this answer for detailed instructions

Don't forget to check for the OS before you append the PostInstallCommand to your cmdclass attribute, you will not want to execute it on your linux envs.

Hope this helps!

like image 53
HassenPy Avatar answered Oct 30 '22 21:10

HassenPy