deploying to heroku from local machine - django

I have django project running on my machine in virtualenv. Is there a way I can deploy my project on Heroku directly? I followed steps mentioned in heroku documentation but i am kind of confused with req.txt since my project already have req.txt in virtualenv. I am new to django heroku techs. Any guidance is highly appreciated.

my project already have req.txt in virtualenv
You should not have req.txt in virtualenv. You should have requirements.txt in your project repository. requirements.txt is just a list of modules that need to be installed in the virtualenv. You create and activate a new virtualenv, then you run pip install -r requirements.txt, and it installs everything you need in the virtualenv. So requirements.txt is not part of the virtualenv, it's more like a description of how to setup the virtualenv.
If this is not clear, you may find my article, virtualenv demystified, useful.

The requirements.txt file defines what python dependencies Heroku will install. Generally speaking, what you use locally will be the same as your local development machine.
If there are differences, you may opt to have an additional file locally that contains local, development-specific packages (for example django-debug-toolbar).

First you will activate your virtual environment and then go to project root and run the command
pip freeze > requirements.txt
This will automatically add all the dependencies of your local machine in requirements.txt. Once you push the file heroku will automatically detect the changes and install them.

Related

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 to bring out django project from virtualenv

I have django project in virtualenv and now I am publishing it in server but the problem is I can not move project from virtualenv, when I do this then related packages inside site-package, cant be read and errors occur, how can I bring out my project from virtualenv without any issuing
Create a new virtualenv on the server. It's easy
Step 1 Get the list of modules in the current virtualenv
source /path/to/current/bin/activate
pip freeze > /tmp/requirements.txt
Step 2 Create a new virtualenv. Login to your new server, copy the requirements file there. Then either change into a suitable directory before excuting the virtualenv command or give a full path.
deactivate
virtualenv -p python envname
Step 3 Install modules
source envname/bin/activate
pip install -r /tmp/requirements.txt
That's it.
As #bruno has pointed out, you really should be using a virtualenv on the server. And you should be using it on your local dev server as well. Then you can be really sure that the code will run at both ends without any surprises.

How to install Django project with all modules?

I created a Django's application which use some additional modules like crispy_forms. I would like to send this application to my friends to test it.
But I don't know how can they just install it and run it? Is it possible?
Application using also database PostgreSQL.
What is the simplest way to just run this application from any place with no errors and problems on the start?
I found only information about https://docs.djangoproject.com/en/1.10/intro/reusable-apps/
and packed my app, but I don't know how to install it.
To setup env for project i would install virtualenv, then:
pip install -r requirements.txt
You need to set database connection in settings.py, or switch to sqlite3...
hope this helps!
If you are using virtual environment then activate it and go in your project root.
If you are not using virtualenvironment then do the same thing, go in your project root.
Make sure you have requirements.txt file.
run the command
pip freeze > requirements.txt
This will add automatically all your modules to requirements.txt file
which can be then installed by
pip install -r requirements.txt

Duplicate the virtual environment for production

This may be a silly question, but I would still like to ask:
I am developing a project using Django, CherryPy, and Nginx. I noticed that a file requirement.txt is usually created to indicate the packages and versions installed in the development environment. Suppose the directory of the virtual environment is /home/me/project/python2Venv.
When I deploy my Django project (tango) into production, the project is copied to the production directory:
sudo cp -r /home/me/project/tango /webapps/tango
For the virtual environment, may I just copy the whole directory using the following command or I should install each of the packages into the production environment again according to requirement.txt?
sudo cp -r /home/me/project/python2Venv /webapps/tango/python2Venv
I think virtualenv uses absolute paths in some files so recreating the env and installing the packages via requirements.txt would be more safe.
In my opinion, it is recommended to install the packages with requirements.txt. Copying directory, can end up being a nightmare.
Say in Update 1:
You have 4 packages each with a specific version(pkg1-ver1, pkg2-ver1, pkg3-ver1, pkg-ver1).
In Update 2:
You have upgraded one package to its new version(pkg1-ver2). With requirements.txt you would just upgrade that one package. Instead of the copying all the packages(Although, i am not sure how well copying of the directory would work).
Hope this helps !
You should install packages with the file requirements.txt.
Or you can use virtualenvwrapper. It helps to clone virtual environments locally easily such as cpvirtualenv, rmvirtualenv, etc.

Django Twilio module is not getting installed on Heroku

I tried below 2 methods to install django_twilio module on Heroku
1) Ran 'heroku run pip install django-twilio'
2) Added 'twilio==3.6.3' to requirements.txt and start the server on heroku.
When I run 'heroku run pip freeze' I can see the twilio entry. But when I go into python and run 'import django_twilio' I get a module not found error.
Please suggest how to fix this on heroku. Same steps worked fine on my local machine.
You didn't add the proper requirement, you only installed the twilio library. Your requirements.txt should include the following line:
django-twilio==0.4
Which will include all the other dependencies you'll need. The full pip freeze, after installing django-twilio looks like this:
Django==1.5.5
django-twilio==0.4
httplib2==0.8
six==1.4.1
twilio==3.6.3
unittest2==0.5.1
As a rule of thumb, always run pip freeze > requirements.txt before pushing an update to Heroku (assuming new dependencies were installed), to make sure you have a complete snapshot of your environment.