You are not logged in.

#1 2010-07-19 16:05:45

baion baion
Member
From: Nicosia, Cyprus
Registered: 2009-06-13
Posts: 48

Distributing Python Scripts

Hello fellow arch users,
I would like to know what is the standard way of distributing a python programm (single file script). I tried to use distutils, and I managed to create a windows executable but failed at creating a linux binary file. I tried

python3 setup.py bdist

but all I get is a tar.gz file with no executable inside. Do I just create a deb or an rpm or a pkg to manage dependencies?
Thank you all in advance.

Last edited by baion baion (2010-07-19 16:15:42)

Offline

#2 2010-07-19 16:17:59

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Distributing Python Scripts

I'm sorry, I can't be of any help, but I was wondering the same thing.

I suppose I would just put the Python file in a directory, add a SH script that has the same name but without the ".py", make it executable, and have it run the program.

As you found out, it doesn't seem to be too hard to make a Windows executable from a Python file.

Offline

#3 2010-07-19 16:59:56

baion baion
Member
From: Nicosia, Cyprus
Registered: 2009-06-13
Posts: 48

Re: Distributing Python Scripts

But what if I want to create a linux executable without releasing the source? I am not planning to do so as I support the open source movement but I would like to know.
And how can I "install" a python program in linux. Do i just copy the .py file in /usr/bin?

Offline

#4 2010-07-19 18:09:00

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Distributing Python Scripts

As far as I know, it's not possible to distribute a Python program without also releasing the source. At least, not easily. I hope someone with more Python experience will correct me if I'm wrong. tongue

As for installation, I believe the standard is to put all of your files in "/usr/share/" and to put an executable SH script to start it in "/usr/bin/".

/usr/bin/MyApplication
/usr/share/MyApplication/MyApplication.py

Offline

#5 2010-07-19 18:25:51

mk12
Member
Registered: 2010-07-06
Posts: 16

Re: Distributing Python Scripts

I don't know that much about this, but what about .pyc files? And why have a shell script to execute a python script instead of a shebang?

Offline

#6 2010-07-19 18:34:19

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Distributing Python Scripts

mk12 wrote:

And why have a shell script to execute a python script instead of a shebang?

So my application doesn't have a silly ".py" extension. tongue

...but, technically, there's no reason to not use a shebang.

Offline

#7 2010-07-19 18:39:42

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Distributing Python Scripts

mk12 wrote:

I don't know that much about this, but what about .pyc files? And why have a shell script to execute a python script instead of a shebang?

I would use the shebang approach as well.

As far as I know, there is no good way to make native executables out of Python code, bar packaging a copy of the source with a statically linked Python executable.  .pyc files require the Python program to run, but technically you could distribute them as a complete system, I think.  (It would be akin to distributing Java .class files.)

Offline

#8 2010-07-19 18:48:34

evilgnome
Member
Registered: 2009-02-20
Posts: 62

Re: Distributing Python Scripts

I've never made a binary distributable from a python script. Maybe I should be using setuptools or distutils or something, but here's what I do in my python program's pkgbuild:

build() {
  cd $srcdir/$pkgname-$pkgver
  install -Dm755 chipmunk-radio.sh ${pkgdir}/usr/bin/$pkgname || return 1
  install -Dm755 chipmunk-radio.py ${pkgdir}/usr/share/$pkgname/$pkgname.py || return 1
  install -Dm755 chipmunk-radio.glade ${pkgdir}/usr/share/$pkgname/$pkgname.glade || return 1
  install -Dm755 chipmunk-radio.desktop ${pkgdir}/usr/share/applications/$pkgname.desktop || return 1
  install -Dm755 chipmunk-radio.png ${pkgdir}/usr/share/icons/hicolor/128x128/apps/$pkgname.png || return 1
}

Offline

#9 2010-07-19 22:40:36

baion baion
Member
From: Nicosia, Cyprus
Registered: 2009-06-13
Posts: 48

Re: Distributing Python Scripts

Can you please explain what that build command does? I am not that familliar with pkgbuilds.
@dcrouzelis: So if I have a single file command line apllication, by placing it inside /usr/bin will I be able to run it from a terminal without navigating to the directory where the source file is? And will I have to invoke python3 application.py or just apllication.py? I don't have a linux machine at the moment to check for myself.

Last edited by baion baion (2010-07-19 22:45:45)

Offline

#10 2010-07-20 00:28:35

user_none
Member
Registered: 2010-03-25
Posts: 10

Re: Distributing Python Scripts

baion baion wrote:

But what if I want to create a linux executable without releasing the source?

You only need to distribute the .pyc files instead of the .py files. However, this will only make it harder. Python is an interperted language so the .pyc files can be turned back into understandable .py files. Similar to decompiling Java .class files.


baion baion wrote:

And how can I "install" a python program in linux. Do i just copy the .py file in /usr/bin?

No. You will put a wrapper script in /usr/bin that will invoke the main script file. Your best bet it to learn distutils for creating install scripts. Take a look at Niw Markdown Editor (a project of mine). It has a simple distutils install setup. For a much more complicated example that has a multi-platform build system (and includes building C based modules) look at calibre.

Offline

#11 2010-07-20 06:37:57

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Distributing Python Scripts

> You only need to distribute the .pyc files instead of the .py files.
+1

Does http://www.py2exe.org/ work? Maybe you can run the app through wine ;-P

Last edited by karol (2010-07-20 06:44:57)

Offline

#12 2010-07-20 22:28:02

baion baion
Member
From: Nicosia, Cyprus
Registered: 2009-06-13
Posts: 48

Re: Distributing Python Scripts

I found something!
It is called cx_freeze and it works with both python 2.7 and python 3.1.1, at least on windows. I will test it on Arch tomorrow and see, although I guess I could just use the .py files and call them with a bash script as dcrouzelis has suggested.

Offline

#13 2010-07-21 19:15:31

evilgnome
Member
Registered: 2009-02-20
Posts: 62

Re: Distributing Python Scripts

baion baion wrote:

Can you please explain what that build command does? I am not that familliar with pkgbuilds.

The source consists of 5 files, and the build command copies each one to a matching directory:
chipmunk-radio.sh
simple launcher script, /usr/bin/chipmunk-radio
chipmunk-radio.py
the program, /usr/share/chipmunk-radio/chipmunk-radio.py
chipmunk-radio.glade
the gui definition, /usr/share/chipmunk-radio/chipmunk-radio.glade
chipmunk-radio.desktop
the launcher item for system menus, /usr/share/applications/chipmunk-radio.desktop
chipmunk-radio.png
the fallback application icon, /usr/share/icons/hicolor/128x128/apps/chipmunk-radio.png

Offline

#14 2010-07-21 21:38:10

baion baion
Member
From: Nicosia, Cyprus
Registered: 2009-06-13
Posts: 48

Re: Distributing Python Scripts

Thank you for your answer. Well if all you want to do is copy the files, then why use the build command? Why not just use the cp command? I assume that the Dm755 parameter has sets the files' access permission?

Last edited by baion baion (2010-07-21 21:38:39)

Offline

#15 2010-07-21 23:44:43

tlvb
Member
From: Sweden
Registered: 2008-10-06
Posts: 297
Website

Re: Distributing Python Scripts

drcouzelis wrote:

So my application doesn't have a silly ".py" extension. tongue

The hashbang has higher precedence than the extension, you don't need the .py.


I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.

Offline

#16 2010-07-22 03:36:25

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Distributing Python Scripts

There is one Python-specific exception: If you want to import your Python file as a module from another Python file, it needs to end in .py and be in the Python path.  However, for a top-level script, I would probably call this a Bad Idea.  For executable scripts, ditch the extension and call it what you want.

Offline

Board footer

Powered by FluxBB