2

I'm trying to change some code in a production server, but when I test the new code it's not being executed. I think the issue is that when I upload the updated source file, it is not being compiled into a .pyc file.

-rw-r--r--  1 ubuntu ubuntu  47872 Jul 13 04:39 admin_email.py
-rw-r--r--  1 root   root    48212 Feb 10 03:12 admin_email.pyc

As you can see from the timestamps above, the .pyc file has not been updated.

Am I correct in assuming the reason the changes in admin_email.py are not being applied is because it is not being compiled? If so, can someone please offer a suggestion on how to get it to do so? Could the issue be that I need to restart the server?

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
evanill80
  • 157
  • 2
  • 11
  • When are .pyc files created? See http://stackoverflow.com/a/15839646/4663466 – Scott Jul 13 '15 at 05:08
  • So it looks like the solution is to delete the compiled file and that should trigger a new compiled version to be created. correct? – evanill80 Jul 13 '15 at 05:17
  • Do you restart the application after you upload the new file? – jgadelange Jul 13 '15 at 05:20
  • That's what I got out of the two answers on the link. – Scott Jul 13 '15 at 05:21
  • jgadelange - Im not exactly sure how to restart application. When in development I usually just restart the dev server, but this is in prod and is running on gunicorn and nginx. Any suggestions? – evanill80 Jul 13 '15 at 05:30
  • In combination with supervisord? – jgadelange Jul 13 '15 at 05:33
  • 5
    Your `.pyc` seems to be readonly to user 'ubuntu' (`-rw-r--r-- 1 root root`). Have you tried deleting the `.pyc` and then accessing the page? – brainless coder Jul 13 '15 at 05:35
  • 1
    It looks like at some point the Python process was run as root, so `admin_email.pyc` is now owned by root and can't be overwritten when the script is run as a regular user. So, delete the existing `.pyc` file and check for other ownership problems. – Marius Jul 13 '15 at 05:37
  • I haven't tried to delete the .pyc file yet. This is on production so I'm concerned about breaking the application. If everyone thinks this is a good idea I guess I will try it! – evanill80 Jul 13 '15 at 05:40
  • It should be safe to delete them, but you can also chmod the files to take ownership instead if you are worried about that. Something like this `find -name "*.pyc" | xargs -i@ sudo chown ubuntu:ubuntu @` – Håken Lid Jul 13 '15 at 07:50

3 Answers3

4

Resetting the application probably does the trick. Since you are using gunicorn/nginx I assume you are also supervisord (if this is not the case please say so, then I can update my answer to also add that). In that case you can do sudo suporvisorctl restart <app_name> to restart the application.

Another issue that might be there, as brainless coder and Marius also stated, is that it seems the application was (at least once) ran as root, you should avoid this. You should remove the .pyc files and change the user the application runs under.

jgadelange
  • 2,482
  • 1
  • 13
  • 10
  • Im not sure if supervisord was used. I did not create the app, I'm updating an existing application and Im still a little green with Django. Maybe the easiest thing to do is backup admin_email.pyc, delete it, then test the application to see if it generates the compiled file and correctly executes. – evanill80 Jul 13 '15 at 08:04
  • Restarting `gunicorn` successfully recompiles `admin_email.py` and updates the applications behavior. You need to execute the command to restart `gunicorn` in the same directory the django script `manage.py` is in. – evanill80 Jul 16 '15 at 06:01
2

The .pyc file is owned by the root user for some reason, and not writeable by other users. Your Python process probably runs as non-root, and can't create new .pyc files.

Either delete the old .pyc files (make a backup first), or chown them to the same user as the process that runs the .py files, and it will probably work.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
1

You can delete it if needed but it's really not. I don't see how that's the reason your code isn't executing.

.pyc files are byte codes that the python interpreter compiles the source to, so as long as you have access to the .py file you're safe to delete.

Are you running the script through shell? Did you give the file executable (+x) permission before doing so?

Leb
  • 15,483
  • 10
  • 56
  • 75