How To Preserve Changes To Pip Installed Django Apps After Deployment - django

I made custom modification to one of the Django apps in my requirements.txt, the problem is that after deployment I get errors because the I get fresh pip installs from the requirement.txt and the changes I made only work locally. What is the right way to modify pip installed Django apps locally and have those changes also reflect in the deployment environment?

You could host a fork of the library you want to change somewhere like GitHub, and have your requirements.txt point to that particular change. http://codeinthehole.com/writing/using-pip-and-requirementstxt-to-install-from-the-head-of-a-github-branch/ has a good overview of having a pip requirements file point to a source code repository.

Related

pipenv updates all dependencies bringing breaking changes

I'm having problems with an app that uses Django. Everything is in a docker container, there is a pipfile and a pipfile.lock. So far, so good.
The problem is when I want to install a new depedency. I open the docker container shell, and I install the dependency with pipenv install <package-name>.
After installing the package, pipenv runs a command to update the pipfile.lock file and doing so updates all packages to their last version, bringing whit these updates a lot of breaking changes.
I don't understand why is this happening, I have all packages listed in my pipfile with ~=, this is suppose to avoid updating to versions that can break your app.
I'll give you an example, I have this dependency in my pipfile: dj-stripe = "~=2.4". But, in the pipfile.lock file, after pipenv runs the command lock, that depedency is updated to its last version (2.5.1).
What am I doing wrong?
Are you sure you're installing it within Docker? A common cause of pipfile.lock conflicts is installing a package locally instead of within Docker and then when the local environment syncs with Docker it will override your pipfile.lock.
Assuming you're using docker-compose, this is how I'm installing my packages:
docker-compose exec web pipenv install <package-name>
I discovered what my problem was.
I've been listing the dependencies like this: ~=2.4, I thought that was indicating not to update to 2.5 or greater, but that's not true, that only tells pipenv not to update to 3 or greater.
In order to stay in 2.4 version, I must specify the last number version, for example: ~=2.4.0
That way, I'm telling pipenv not to update from 2.4.

Heroku app crashes after pushing change with new dependencies

So I have a Django web app running on Heroku. I recently pushed an update where I added some API functionality via the Django Rest Framework. I installed this via pip. Now my Heroku app crashes. I assume I somehow need to install this dependency on Heroku? Here is the error message:
An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command
heroku logs --tail
I tried entering the Heroku CLI and typing pip install djangorestframework but and running Heroku restart but it still crashes.
Edit: some more details, I tried installing this dependency on my machine via git clone in addition to pip. When I pushed this code to Heroku, in the command line I see cannot stat '/tmp/build_5917e4123a7c/requirements.txt': No such file or directory
Edit2: Just to keep this post updated, I'm not trying to add a requirements.txt file to my project with this dependency in it. The file is in the root directory and the only text in it is:
djangorestframework==3.11.0
Edit3: I fixed it. See my answer
So I installed my dependency with pip install djangorestframework but I needed to use pipenv install djangorestframework. The Pipfile therefore never had this dependency added so Heroku wasn't able to see it since it seems Heroku checks the Pipfile. I guess requirements.txt is the old deprecated way to do this?

How to add a new pip package to a deployed application with heroku

I have deployed a django rest api with heroku. And I have some updates regarding the code, For updating the source code is easy just updating the source code and commit changes with git. But I have trouble adding a new pip package. Is there a solution for this problem or do I need to re-deploy the application?
All you need to do is to install the package in your virtual environment and using the pip freeze > requirements.txt command, update the requirements text file.
Once your done, commit the changes and push to heroku or github (depending on your setup)

confusion in deploying module's with django

Good day.
I'm a newbie to Django and I have a slight confusion:
When deploying my Django app, do I need to deploy it with all the Python 'come-with' modules, or the hosts already have them installed.
Also, I installed PIL for image manipulation. Would they also have it installed or i have to find a way to install it on their servers. Thanks in advance
do I need to deploy it with all the Python 'come-with' modules
Never do that. It might conflict with the dependencies on the server. Instead issue the following command to create a dependency file (requirements.txt).
pip freeze > requirements.txt (issue this command where manage.py is located)
On the server create a new virtual environment. Now copy django project to the server (you can do this using git clone or just plain old Filezilla). Activate virtual environment. Then change you current working directory to the where manage.py is located. to install all the dependencies issue the following command.
pip install -r requirements.txt
This will install the required dependencies on on server.

How can I uninstall Django apps?

So I was installing an app in order to bootstrap my Django admin interface, and I thought this app was going to be Project specific but it appears to be installed on a global Django level.
http://riccardo.forina.me/bootstrap-your-django-admin-in-3-minutes/
https://github.com/riccardo-forina/django-admin-bootstrapped
My question is how can I uninstall it if I need to do so at a later date? I wanted my project to be as independent as possible. I was also wondering if there was a way of doing the installation within the project so that people that download my repository will automatically get it.
Also some minor questions are that after adding "add django_admin_bootstrapped into the INSTALLED_APPS list before django.contrib.admin" I was not required to run a syncdb command like we usually are when installing models. I guess this applications doesn't creates tables on my database so that is probably why, but I just wanted to know your thoughts.
I know it is a lot to answer but any clarification is appreciated. Thanks.
If you have installed the django app using pip do:
pip uninstall app_name
Or you have to go manually to your site-packages directory and remove it.
After that,
Remove the app from INSTALLED_APPS. django-admin-boostrapped might have overridden your templates. After you are done, do ./manage.py collectstatic and ./manage.py syncdb
If you're writing something that you want other people to use, and it relies on other packages (whether Django apps or more generic Python packages) it's standard to use pip. This makes it easy to install and uninstall packages, and specific versions of those packages. You can then create a requirements.txt file, which you include with your project. This lets other people know what packages are required, and they can easily install them using pip.
So, first off, install pip.
Then you would install django-admin-bootstrapped by doing:
$ pip install django-admin-bootstrapped
You can also install django using pip:
$ pip install django
If you then do this:
$ pip freeze > requirements.txt
you'll end up with a requirements.txt file that lists any packages you've installed with pip, and which version of each. Include that file with your project when you share it with others (on GitHub or wherever). Those people would then do this, to install the same packages:
$ pip install -r requirements.txt
It would also be worth installing and using virtualenv – this lets you have separate environments for your python work, so that when you pip install something it's only available in that environment. Then you can have different versions of packages (eg, different versions of Django) in each environment. Virtualenvwrapper also makes some of the common virtualenv tasks a little easier.
It's a lot to get to grips with at first, as I know from experience, but it's worth doing so and will make development easier in the long term.
As to your other question, it looks like django-admin-bootstrapped doesn't have any models, so it doesn't require any updating of the database after installation.