I am running into a problem where any changes I make to my .py Django files don't change the .pyc file, so the Django web site doesn't change until I manually delete the .pyc file.
This only happens when running a Django app/py files dependent on Django.
Example:
models.py: contains methods to update database
cron.py: Independent script that sets up Django environment and then uses models.py to update database as a cron job
For some reason, changes in cron.py are reflected when I run it with python cron.py but cron.py will NOT pick up any changes I make to models.py. It will run off of the old .pyc file which is consistent with that happens when I use models.py through the web app.
I've tried chmod 777 on the entire directory. Does anyone have any idea what is causing this? Is there any relevant code I can post?
In case you are running this your Django instance with apache, you will need to restart apache in order for the changes to reflect, which you can simply do using
sudo service apache2 graceful
The pyc files are autoupdated locally with the default django development server only.
Related
I am developing a small web application using Django and Elasticbeanstalk.
I created a EB application with two environments (staging and production), created a RDS instance and assigned it to my EB environments.
For development I use a local database, because deploying to AWS takes quite some time.
However, I am having troubles with the migrations. Because I develop and test locally every couple of minutes, I tend to have different migrations locally and on the two environments.
So once I deploy the current version of the app to a certain environment, the "manage.py migrate" fails most of the times because tables already exist or do not exist even though they should (because another environment already created the tables).
So I was wondering how to handle the migration process when using multiple environments for development, staging and production with some common and some exclusive database instances that might not reflect the same structure all the time?
Should I exclude the migration files from the code repository and the eb deployment and run makemigrations & migrate after every deployment? Should I not run migrations automatically using the .ebextensions and apply all the migrations manually through one of the instances?
What's the recommended way of using the same Django application with different database instances on different environments?
Seems that you might have deleted the table or migrations at some point of time.
When you run makemigrations, django create migratins and when you run migrate, it creates database whichever is specified in settings file.
One thing is if you keep on creating migrations and do not run it in a particular database, it will be absolutely fine. Whenever you switch to databsse and run migrations, it will handle it as every database will store the point upto which migrations have been run until now in django-migrations table and will start running next migrations only.
To solve your problem, you can delete all databases and migration files and start afresh as you are perhaps testing right now. Things will go fine untill you delete a migration or a database in any of the server.
If you have precious data, you should get into migration files and tables to analyse and manage things.
I am working on a Django based application whose location on my disk is home/user/Documents/project/application. Now this application takes in some values from the user and writes them into a file located in a folder which is under the project directory i.e home/user/Documents/project/folder/file. While running the development server using the command python manage.py runserver everything worked fine, however after deployment the application/views.py which accesses the file via open('folder/path','w') is not able to access it anymore, because by default it looks in var/www folder when deployed via apache2 server using mod_wsgi.
Now, I am not putting the folder into /var/www because it is not a good practise to put any python code there as it might become readable clients which is a major security threat. Please let me know, how can I point the deployed application to read and write to correct file.
The real solution is to install your data files in /srv/data/myapp or some such so that you can give the webserver user correct permissions to only those directories. Whether you choose to put your code in /var/www or not, is a separate question, but I would suggest putting at least your wsgi file there (and, of course, specifying your <DocumentRoot..> correctly.
We use Django for a project. Prior to 1.7+, we were able to dynamically pull in files to our python environment when setup.py was being executed. For example, we have this directory structure:
/foo/bar
/foo/bar/__init__.py
/foo/bar/my_functions.py
Our Django project doesn't know anything about those files to start off. When the web server starts and setup.py is read, we look at a configuration which tells us where to find files to add to our environment. Again, let's assume /foo/bar is in our configuration to be dynamically loaded. We would then use import_module() to import it so that anything under /foo/bar essentially becomes part of the project and can be used. It's basically a "plugins" feature.
After Django >=1.7, this causes huge problems, mainly:
django.core.exceptions.AppRegistryNotReady: The translation infrastructure
cannot be initialized before the apps registry is ready. Check that you
don't make non-lazy gettext calls at import time.
It also limits our ability to add new files dynamically as you always have to put them in place and restart your web server. You couldn't have an "Upload Plugin" page to add new files to the server, for example.
Is there a way to add files like this both during the web server startup as well as after the startup without restarting it?
I have experience with PHP but now have to manage a python application. It is already developed and running on live server. On live server we have beta.domain.com as well. which has its own copy of same source, other than the main domain application.
Now, when we print something within template it effects but before that, following MVC, if we try to print or use sys.exit() in manage.py or later imported "settings.py" or then views.py, nothing effects these files at all. After any change in just mentioned files, Website still renders everything and display related template.
Another thing that if even we remove
return render_to_response('home.html', RequestContext(request, context))
inside the views.py for testing, nothing effects and website still gets rendered with template.
Do I need to think that if website code is already hosted then in order to have changes to reflect new one in the code in any file (.py), needs to be reinitialized by executing any project related file?
I am not sure how python code needs to be updated at all, any quick help is much appreciated.
Anytime you change your code, you need to restart Apache server.
<path to apache>/bin/apachectl restart
The python interpreter of the process has already loaded your python modules in previous web requests. And once the module is loaded, it is stored in memory. Next time when a request comes, the Python interpreter will simply use the version of the module that is already loaded in memory. So your changed code will not be picked up.
Please, please, please don't try and edit files on your live server. You're only going to get yourself into terrible difficulties. Use a local copy for development, preferably cloned via a version control system, and deploy at regular intervals rather than on every change.
To answer your question, you usually need to restart the WSGI process in order to see Python code changes. An easy way to do this is to touch the .wsgi file, at which point mod_wsgi will detect that it has changed, and reload everything. Otherwise you can simply reload Apache.
Unfortunately I don't understand your references to manage.py or sys.exit.
The solution is excatly restarting the apache server as answered by Sudipta .Each time the py files are interpretted .pvc files are created for each corresponding .py files.When the Apache server is restarted all the python source files are interprretted again and new .pvc files are generated and only these files will run till the apache restarts again.
recently I started a small Django project that I developed on a local machine using a SQLite3 database and the integrated development server. I now copied the whole project to a server running Debian.
Everything worked well as long as I kept using the SQLite3 database. Now I wanted to switch to a local MySQL database, so I changed the settings.py file in my project's root folder, created the database and added a user. I then ran syncdb and it created the needed tables without any problems.
Now I wanted to use the app, but I keep getting errors, because Django can not find the tables - neither the 'standard tables' like django_sessions nor my own tables - even though they are there (I checked the database).
The one thing that seems awkward about the DatabaseError pages I get is the following line:
Exception Location: /usr/local/lib/python2.6/dist-packages/django/db/backends/sqlite3/base.py in execute, line 234
It seems like Django is still using the SQLite3 backend even though I set it to use the MySQL backend.
The other thing that nearly makes me freak out is this: I deleted the data.sqlite file in my app's root folder using rm. But when I use my app, the file is being recreated!
Can anyone tell me where I went wrong?
When running in production with mod_wsgi or mod_python, new code isn't incorporated until you reload/restart the webserver.
If you are using apache with mod_wsgi, you can also touch the wsgi.py file to restart the python code only.
If running on apache run "sudo /etc/init.d/apache2 reload" everytime you make a change.