How to create python module and make available for any application

Asked

Viewed 459 times

0

I wish my python program could be put into the system in such a way that with a simple import it can be executed, for example:

import meucodigo
meucodigo.main()

My code contains several files (own modules) and all are called through the main class.

The idea is that with this I can create an installer so that it configures everything correctly for the end user. I intend to use the install Creator 2 and so the user can call it after the installation. Remembering that I need it to be an installer like the one mentioned. Some tip?

I know it is possible, but unfortunately I can not find any tutorial to teach.

NOTE: Use and need to use python 2.5.

  • 1

    To create your own python Packages see that answer and to "package" your app into an installer, see this one, here at Stopt.

  • The way that is being taught in link 1 I already use, however, I wish that all my code could be imported by another program, being called in any part of the operating system.

  • 1

    So what you want is to create and distribute your packages, you can publish in Pypi or on a server itself, is quite extensive to put an answer here, start here, if you have difficulties with English, try a google search like this.

  • I saw this possibility, but as I said, I would like it to be configured through an installer, because the script created is proprietary and needs to be run by another program called Arcgis. Understand?! A while ago I saw a video that taught basically this but unfortunately I did not find more. But thanks for the tip!

  • Nothing prevents you from configuring and installing a package through an installer.

1 answer

1


Create the following folder structure:

MeuCodigo\
   README
   LICENSE
   setup.py
   meucodigo\
       __init__.py
       meucodigo.py

Inside setup.py put the following:

from distutils.core import setup
setup(name='MeuCodigo', version='0.1', author='Edeson Bizerril',
    author_email='[email protected]', 
    url='http://bizerril.com/meucodigo',
    packages=['meucodigo'],
)

Within __init__.py put the following:

from .meucodigo import *

Within meucodigo.py put the following:

def main():
     # codigo aqui
def .... # outras funções etc

Then open a command prompt, go to the folder MeuCodigo and type:

C:\MeuCodigo> py setup.py bdist_wininst

This generates an installer executable on MeuCodigo\dist\MeuCodigo-0.1.exe

You can use too:

C:\MeuCodigo> py setup.py bdist_msi

To generate the installer in msi format MeuCodigo-0.1.msi.

See more on the subject at documentation of distutils.

  • My main interest is that he may be summoned through command import meucodigo by any program or terminal. It is possible after this installation?

  • 1

    @Edesonbizerril yes, any program or terminal running the python of course. import meucodigo is not valid windows powershell or CMD command.

  • I made the following mistake: C:\Users\Edeson Bizerril\Desktop\meu_teste>py setup.py bdist_wininst
'py' não é reconhecido como um comando interno
ou externo, um programa operável ou um arquivo em lotes. My python installed is not the default, but one that came installed with the Arcgis program in the directory: C:\Python27\ArcGIS10.5

  • 1

    @Edesonbizerril py is the python launcher for windows maybe you’re not in your PATH so you don’t think so. Try specifying the full path for your python, for example, type: C:\Python37\python.exe setup.py bdist_wininst (if your python is installed in C:\Python37)

  • It worked really well. The last question: I did the test with a single file, but if I have multiple directories and files it will do the installation correctly?

  • 1

    @Edesonbizerril yes, as long as everyone is inside your package (meucodigo folder) and all Packages are correctly specified in setup.py. There are parameters also to specify the installation of additional files other than python code, among other useful functions. Read the documentation of distutils.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.