12

Is it possible to stop Django from creating the .pyc files? I know it's Python that creates them when modules are imported, but is there some way of turning them off?

Totally understand why they're created, etc, and will want them when I go live, but currently they just clutter up the directory and it bothers me - so please, no "you shouldn't want to turn them off". I also know how I could stop them appearing, etc, etc. I really just want to know how I can stop them being created.

Oh and I'm on Linux, of course.

Thanks.

  • 2
    Practically a duplicate of this question: http://stackoverflow.com/questions/154443/how-to-avoid-pyc-files, try searching before asking a question – ashwoods May 15 '11 at 13:36
  • 2
    Django has no say in the matter. It's completely in Python's domain of responsibility. – Mike DeSimone May 15 '11 at 13:58
  • 3
    I didn't want to come over rude, just stating a fact. I'm not sure you really "totally understand" why they are created. You do know django is "just" a python script, and it is python who is creating the pyc files. Every python program run will create pyc files, so you just have to do python -B manage.py [command], as with any other python script. Or do you think we need a question on stack for "is it posible to stop [my python program this time] from creating pyc files"? http://stackoverflow.com/questions/how-to-ask – ashwoods May 15 '11 at 14:01
  • I do realise that, yes. However, it's much better practice to configure the framework to do such (if possible) which is why I asked the question. –  May 15 '11 at 14:07
  • 1
    ... It has nothing to do with the framework. It's not in django, its not part of wsgi, its not anywhere close. 'Django' is not creating pyc files, its the python interpreter. We could of course make a question for each interpreter option * each python program out there. If we take it that the interpreter takes around 17 arguments, and there are thousands of python programs, well, you get the point. – ashwoods May 15 '11 at 15:57
  • and my last tip for the day: best practise is to have a virtualenv for your django project and that you set whatever development variables there, not in manage.py. – ashwoods May 15 '11 at 16:07
  • Ashwoods, well done, I'm aware of that. However all framework settings - where possible - should be independent of the OS/language config. You should be able to pick up all your files, plonk them on another server and they should work. That's the kind of response I was looking for. One that instructs me how to do this not in a *python* way, but in a *django* way. There is abstraction for a **reason**. I am completely aware that PYC files are created by **python**. **Completely aware**. You seem, however, to be *unaware* of good framework practice. –  May 15 '11 at 18:26
  • i'd love to see an example in any popular python "framework", where your good practises are used. From what you are saying, I guess you come from php, where you pick up your files, plonk them on another server, and they should work. Decoupling and separation of concerns? you want to separate dev/producition settings from the app, as well as the environment. That's why *your* "-b" should go in the virtualenv, and if you want to automate you dev/prod settings, use a tool like fab, in django, or... any other python program ;) but lets just agree that we don't agree on python best practises – ashwoods May 16 '11 at 18:40
  • this is a duplicate of http://stackoverflow.com/questions/154443/how-to-avoid-pyc-files - which will let you know several ways to turn off `.pyc` generation; from one time only with commandline option to always with something you set in your environment. – dnozay Jul 15 '13 at 21:15

4 Answers4

10

You can use this, where applicable:

import sys

sys.dont_write_bytecode = True
Helbreder
  • 882
  • 2
  • 13
  • 24
  • Correct me if I'm wrong, but this would need to be done in the Django project's `manage.py` file as well as any WSGI files. Are there other places that need this set? – Mike DeSimone May 15 '11 at 14:01
  • I'm going to look into this a little before I set this as the answer, but this looks to be what I was looking for, pretty much. –  May 15 '11 at 14:08
  • Worked perfectly, put that inside manage.py and it was fine. Not using django with Apache yet, so that's ok. –  May 15 '11 at 14:35
  • I think it can be set in django/__init__.py file once, to prevent all django's imports from trying to create .pyc files. – Helbreder May 15 '11 at 14:37
9

You can try setting the PYTHONDONTWRITEBYTECODE environment variable:

PYTHONDONTWRITEBYTECODE

If this is set, Python won’t try to write .pyc or .pyo files on the import of source modules.

New in version 2.6.

Community
  • 1
  • 1
alexandrul
  • 12,856
  • 13
  • 72
  • 99
4

Very late reply but I got here after Googling. You can try this:

python -B manage.py [any other commands/options]

For example:

python -B manage.py sql yourapp

However, this doesn't work for some reason:

python -B manage.py runserver 0.0.0.0:5000
Samhain13
  • 71
  • 4
2

Edit your dispatcher, so the hashbang reads:

#!/usr/bin/env python -B

bluepnume
  • 16,460
  • 8
  • 38
  • 48
  • The operative question then being: does WSGI (e.g. Apache's mod_wsgi) launch the WSGI file using the shell? If it imports the WSGI file as a Python module, this won't work. – Mike DeSimone May 15 '11 at 14:01
  • Is there any documentation tat I can look for this `-B` argument? – Underoos May 31 '20 at 07:38