8

Is there a way to stop python from creating .pyc files, already in the shebang (or magic number if you will) of the Python script?

Not working:

#!/usr/bin/env python -B
4
  • 2
    @VajkHermecz, no my question is much more specific. Commented Nov 14, 2012 at 10:02
  • Yeah, sorry related would have been a better term... srry Commented Nov 14, 2012 at 10:07
  • 2
    Voting to reopen because it has different implications than the duplicate. Commented Jul 21, 2015 at 20:40
  • The so called "duplicate" is a quite completely different question which does not have any answer related to shebang. Commented Mar 8, 2021 at 20:44

4 Answers 4

7

it is possible by putting your python interperter path directly in the she bang instead of using env.

#!/usr/bin/python -B

of course this means you lose out on some of the portability benefits of using env. There is a discussion of this issue with env on the wikipedia Shebang page. They use python as one of their env examples.

Sign up to request clarification or add additional context in comments.

Comments

4

According to the man page for env, you can pass name=value to set environment variables. The PYTHONDONTWRITEBYTECODE environment variable causes Python to not write .py[co] files (the same as the -B flag to python). So using

#!/usr/bin/env PYTHONDONTWRITEBYTECODE=1 python

should do the trick.

EDIT:

I tested this with a simple Python script:

#!/usr/bin/env PYTHONDONTWRITEBYTECODE=1 python
print 1

then

$chmod +x test.py
$./test.py
1
$ls
test.py

(but not test.pyc)

4 Comments

For me the script just hangs...
Does it work with just python -B script.py? I just tested a simple script, and it seemed to work OK to me.
me too. this doesn't seem to work.
Yes, it works with python -B script.py
4

Yes, if and only if, we assume the Python program runs in a somewhat POSIX compatible system (for /bin/sh), this will work:

(IMPROVED based on input from glglgl)

#!/bin/sh
"exec" "python" "-B" "$0" "$@"

# The rest of the Python program follows below:

8 Comments

Note to self: later improve with stackoverflow.com/a/9057699/193892
Looks great! The "s are for the shell and ignored by python...
But it breaks syntax highlighting...
@glglgl, haha, I though it might. Will it appear again if the script is called something.py ? :)
Yes, that would do it. But then the script has a .py extension... not nice either, but probably you'll have to die one death...
|
3

Alas, no. The shebang stuff is limited to giving an executable and one parameter.

So env tries to execute python -B with the given file as one argument instead of python with -B and the current file as two arguments.

I don't see a way to achieve the wanted goal.

1 Comment

The funny part is that Python Windows Launcher doesn't know about this limitation and passes the options (un)correctly. So for once Python on Window works better ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.