4

I have a compiled Python 2.6 program on Linux called abc.pyc. If I run

./abc.pyc

it runs fine. However if I rename it to abc (i.e, without the pyc extension) it fails to run. It gives the error message:

ImportError: No module named abc

How can I run my program without the .pyc extension?

UPDATED: I want to run the compiled (pyc) version not the py version

Alternatively, I know I can compile modules to .so files using Cython, but can I compile my main Python program to something I can execute on a Linux platform? If so, a Cython command line example would be appreciated.

UPDATED I've raised the Cython question as a separate question.

mozza
  • 965
  • 1
  • 9
  • 9
  • 1
    Useless question: why do you want to run it without the extension? – Andrei Duma Feb 24 '11 at 13:30
  • Becasue I want to add a little obfuscation - I know it's not much, but I would like it. – mozza Feb 24 '11 at 13:37
  • 3
    @mozza: Obfuscation to what end? This is not going to help you. See an existing discussion on protecting Python: http://stackoverflow.com/q/261638/118160 – Seth Johnson Feb 24 '11 at 13:39
  • I know it's not very good, and easy to get around, but if it's possible I'd like to do it. Alternatively see my updated question re using Cython. – mozza Feb 24 '11 at 13:47
  • @mozza: very, very few Python programs can be compiled using Cython without major changes. – Seth Johnson Feb 24 '11 at 14:01
  • @Seth Johnson: I compiled 5 Python modules to .so files with just a few minor changes. Guess I was lucky. – mozza Feb 24 '11 at 14:06

3 Answers3

6

Create a file named 'abc' which contains:

#!/bin/sh
/usr/bin/env python -m abc

or is that giving away too much valuable source code if they can read that?

Note also that .pyc files aren't portable between different versions of Python. Redistributing the source really would be best for that reason quite apart from causing less annoyance to your users and less cost to yourself (in support time if nothing else).

Duncan
  • 92,073
  • 11
  • 122
  • 156
  • If using `-m`, you don't want the `pyc` extension (just the `abc` module name) – ncoghlan Feb 24 '11 at 21:58
  • Thanks, I've corrected that. I've yet to find any situation where I'd want to use `-m` so I'm not actually that used to it. – Duncan Feb 25 '11 at 08:19
1

Don't rename the .pyc file - that is just a shortcut for the interpreter, so that it doesn't have to recompile already compiled code.

Instead, rename the .py file to abc, chmod +x ./abc and make sure the first line reads:

#!/usr/bin/python

or even

#!/usr/bin/env python

From your explanation of the problem, I guess the .pyc file contains some linux loadable code that lets it be run as a binary, providing it can check the .py file for newer versions - and probably just starts the python interpreter to load itself or the source file (whichever is newer) and then go on with that. Who knows? Maybe the os specific docs will tell you. Maybe they won't.

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
1

If you're looking to obfuscate your code, see this discussion on protecting Python code. Basically, you can't wantonly distribute the Python bytecode which comprises the .pyc file, and there's no way to prevent people from peeking into your code.

Community
  • 1
  • 1
Seth Johnson
  • 14,762
  • 6
  • 59
  • 85