'Django Heroku : python: can't open file 'manage.py': [Errno 2] No such file or directory

I am deploying a Django website on Heroku. My project is called mysite-project wich contains at its root manage.py and Procfile

I can visit the heroku website after I run git push heroku master.

And the website shows:

enter image description here

I am assuming I do not see anything (navbar, initial page etc) because I did not run migrate.

If I do:

heroku run python manage.py migrate

I get the error:

python: can't open file 'manage.py': [Errno 2] No such file or directory

Which makes no sense since I am in the right directory. In fact:

  • I can run python manage.py runserver and locahost works
  • git ls-files manage.py --outputs--> manage.py

BUT If I do:

heroku run bash
ls manage.py

I get: ls: cannot access 'manage.py': No such file or directory

It seems that manage.py is in my local but not in my heroku.

Procfile

web: gunicorn mysite-project.wsgi


Solution 1:[1]

First use heroku logs --source app --tail to make sure that the Build succeeded. If the build is successful, make sure you have your project at the root of the git repo. I had the same problem because my Django project was not at the root of the git repo. My file hierarchy was first like (assuming the name of the project prototype and it has one app named app)
???git_repo
        ???Procfile
        ???requirements.txt
        ???new_folder
                ???prototype (folder)
                ???app (folder)
                ???.gitignore
                ???manage.py
                ???db.sqlite3

I changed the hierarchy to the following and it worked
???git_repo
        ???Procfile
        ???requirements.txt
        ???prototype (folder)
        ???app (folder)
        ???.gitignore
        ???manage.py
        ???db.sqlite3

Solution 2:[2]

As pointed out by @Reem_Abdel_Rahman, your project's manage.py file is probably not present at the root of your project's git repository.

However, you can still fix this issue if you provide the relative path of the manage.py file in your project's Procfile as:

web: python project-name/manage.py runserver 0.0.0.0:$PORT

Also, make sure that you specify 0.0.0.0:$PORT at the end to allow your django app to bind to any IP which by default binds only to localhost.

Solution 3:[3]

The welcome you see when you visit the site (as well as the fact, that you can't find manage.py with ls on the server) tells you, that you haven't successfully pushed your django project to Heroku yet. Try and run git push heroku master.

It may be that the push has failed, in which case you should consult the documentation. Specifically, run

$ pip install gunicorn django-heroku
$ pip freeze > requirements.txt

...and then add

import django_heroku
django_heroku.settings(locals())

...to the bottom of settings.py. Also don't forget to set STATIC_ROOT='staticfiles' in settings.pyaswell.

Solution 4:[4]

That screenshot attached indicates you have just created app in heroku but not pushed your local repository code to heroku app.

git push heroku master

Solution 5:[5]

I was getting this error too! I solved it by specifying the path:

python <your_project_name>/manage.py migrate

This worked for me.

Solution 6:[6]

In my case, it was because I was using a branch name other than master. My branch name was heroku_branch, so to make things work properly, I pushed it this way

git push heroku heroku_branch:master

It was pushed this time and worked fine.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Reem Abdel-Rahman
Solution 2 Hemiacetal
Solution 3 andreashhp
Solution 4 Aishwarya kabadi
Solution 5 crish
Solution 6 Muhammad Zubair