Python PY files do not reflect changes - python

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.

Related

Changes in flask based python project scripts are completely ignored during execution

I am working on a python based micro service. Now, changes in any of the scripts used besides the application.py script are completely ignored. Unfortunately I have no idea what changes caused this problem but it broke after moving the Project directory locally and restructuring the file structure.
Has anyone experienced this before and found a solution?
Fortunately I found the answer myself. The Problem was that I didn't update the OpenAPI file which links the endpoints to the respective python functions after changing the file-structure of the project!

Flask doesn't seem to recognize file changes

A little background:
I've been working on this project for about six months now and it's been running on Flask the whole time. Everything has been fine, multiple versions of the backend have been deployed live to support an app that's been in production for months now.
The development cycle involves writing everything locally and using Flask-Script's runserver command to test everything locally on localhost:8080 before deploying to a dev server and then finally to the live server.
The Problem: The other day my local flask instance, which runs on localhost:8080 apparently stopped respecting my local files.
I tried adding a new view (with a new template) and I got a 404 error when trying to view it in my browser.
I then tried making some test changes to one of the existing pages by adding a few extra words to the title. I restarted flask and none of those changes appeared.
I then went as far as deleting the entire views.py file. After restarting flask again, much to my dismay, I could still view the pages that were there originally (i.e. before this behavior started).
Finally, I made some changes to the manage.py file, which is where I put all of the Flask-Script commands, and they weren't recognized either. It's as if flask started reading from a cached version of the filesystem that won't update (which very well might be the case but I have no idea why it started doing this or how to fix the issue).
FYI: Browser caching shouldn't be an issue b/c I have the dev tools open with caching disabled. Plus the fact that changes to manage.py aren't being noticed shouldn't have anything to do with the browser.
You've most likely used flask in the DEBUG mode in which it auto reloads templates the app whenever a file changes.
Try using
export FLASK_DEBUG=True
before running
flask run
For more information see http://flask.pocoo.org/docs/1.0/config/#DEBUG
I was having a similar issue and deleting the .pyc files solved it for me.

Django deployed app/project can't write to a file

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.

How to reload a python library in a flask, nginx, uwsgi stack?

I've written some code for an image reconstruction server. I use a python library but it doesn't work for server so I made some changes so that it would work but it never reloads the library. I make the changes and it ignores them. I don't understand how this can be possible? Where does it get this information from and how can I make it get a fresh copy of the library every time?
The Python process started by uWSGI only interprets the source once at startup, any code changes after that will not affect the in-memory process. You should probably just manually restart the uWSGI process when this happens. Alternatively you can tell uWSGI to auto-reload if files or directories change: http://uwsgi-docs.readthedocs.org/en/latest/Options.html#touch-reload.

Web2Py Working Directory

Well I want to use WEb2Py because it's pretty nice..
I just need to change the working directory to the directory where all my modules/libraries/apps are so i can use them. I want to be able to import my real program when I use the web2py interface/applications. I need to do this instead of putting all my apps and stuff inside the Web2Py folder... I'm trying to give my program a web frontend without putting the program in the Web2Py folder.. Sorry if this is hard to understand .
In any multi-threaded Python program (and not only Python) you should not use os.chdir and you should not change sys.path when you have more than one thread running. It is not safe because it affects other threads. Moreover you should not sys.path.append() in a loop because it may explode.
All web frameworks are multi-threaded and requests are executed in a loop. Some web frameworks do not allow you to install/un-install applications without restarting the web server and therefore IF os.chdir/sys.path.append are only executed at startup then there is no problem.
In web2py we want to be able to install/uninstall applications without restarting the web server. We want apps to be very dynamical (for example define models based on information provided with the http request). We want each app to have its own models folder and we want complete separation between apps so that if two apps need to different versions of the same module, they do not conflict with each other, so we provide APIs to do so (request.folder, local_import).
You can still use the normal os.chdir and sys.path.append but you should do it outside threads (and this is not a web2py specific issue). You can use import anywhere you like as you would in any other Python program.
I strongly suggest moving this discussion to the web2py mailing list.
os.chdir lets you change the OS's working directory, but for your purposes (enabling imports of a bunch of modules &c which are constrained to live in some strange place) it seems better to add the needed directories to sys.path instead.
I had to do this very thing. I have few modules that I wanted to use from my controllers. If you want to be able to use the code that resides in the modules directory in the controller, you can use:
# use this in your controller code
impname = local_import('module_in_modules', reload=True)
# reload true will ensure that it will re load whenever
# there are changes to the module
Jay

Categories

Resources