run manage.py where manage.py is not present? - django

manage.py is under project_home/mysite/mysite/bin/manage.py
I did it following a recommendation from some book.
The point is it's rather hard to execute python manage.py something as it stands since I have to move to the directory or type the directory path leading to it.
Is there a convenient way to call manage.py in this setup?

If you want to call manage.py anywhere, you can use Django-manage.py-anywhere. As package description says you can run manage.py commands from any directory of your Django project.
See more here: https://github.com/timonweb/Django-manage.py-anywhere

Related

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 which 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:
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
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
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.
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
I was getting this error too!
I solved it by specifying the path:
python <your_project_name>/manage.py migrate
This worked for me.
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.
For django + heroku
web: python manage.py runserver 0.0.0.0:\$PORT

Location of manage.py file for django-disqus

Everything on the install of django-disqus has gone smooth till now.
I ran manage.py as instructed to here: http://django-disqus.readthedocs.org/en/latest/commands.html but the output was all wrong because I ran the dumpdata command on the wrong manage.py file.
./manage.py dumpdata
Where is the manage.py file I'm to run dumpdata on?
I'm attempting to setup the dump as a .json so that the comments are pulled locally as seen here: http://docs.disqus.com/help/58 It keeps a backup and is good for seo.
The docs you linked are to custom management commands that django disqus adds...
You use the project's ./manage.py, but now it has added functionality by typing:
./manage.py disqus_dumpdata

manage.py runserver - ImportError: No module named MYSITE

I can't get the dev environment running!
I've been following the instructions here (tutorial on DjangoProject page). When I type
manage.py runserver
I get the following error:
ImportError: No module named MYSITE
I am executing the command within the folder MYSITE which has the files init.py, manage.py, settings.py, urls.py
I've searched around and found questions on "manage.py runserver", but not this specific error. Your help is greatly appreciated. Thanks
If you're using windows, you'll need to put your project's directory (the one with manage.py in it) into an environment variable called PYTHONPATH.
Since this is highly ranked on Google and I got here while searching for an answer I'll share my solution:
I have a project built on my machine, packaged into a debian package and installed on a test machine. On my machine I use the runserver but on the testmachine I use apache (which was using fine). When changing database from sqlite to postgresql I could not do shell or syncdb etc (all manage commands). The problem here was that manage.py was owned by www-data and it had to be root for these commands to work...
sudo chown root. manage.py
Another different answer ;) I'm walking the "Django for the Impatient: Building a Blog" chapter from "Python Web Development with Django" and it suggests creating a "dotted" module so I registered my application as "x.y" which it didn't like. Changing to "y" which matched the file-system as:
/x
+ manage.py
+ /x
+ settings.py
+ /y
+ models.py
Worked for me.

Django: no such table: django_session

I have found several topics with this title, but none of their solutions worked for me. I have two Django sites running on my server, both through Apache using different virtualhosts on two ports fed by my Nginx frontend (using for static files). One site uses MySql and runs just fine. The other uses Sqlite3 and gets the error in the title.
I downloaded a copy of sqlite.exe and looked at the mysite.sqlite3 (SQLite database in this directory) file and there is indeed a django_session table with valid data in it. I have the sqlite.exe in my system32 as well as the site-packages folder in my Python path.
Here is a section of my settings.py file:
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'mysite.sqlite3', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
I did use the python manage.py syncdb with no errors and just a "No Fixtures" comment.
Does anyone have any ideas what else might be going on here? I'm considering just transferring everything over to my old pal MySql and just ignoring Sqlite, as really it's always given me some kind of trouble. I was only using it for the benefit of knowing it anyway. I have no overwhelming reason why I should use it. But again, just for my edification does anyone know what this problem is? I don't like to give up.
After made any changes in code, run the following commands
manage.py makemigrations
manage.py migrate
it worked for me.
It could be that the server uses a different working directory than the manage.py command. Since you provide a relative path to the sqlite database, it is created in the working directory. Try it with an absolute path, e.g.:
'NAME': '/tmp/mysite.sqlite3',
Remember that you have to either run ./manage.py syncdb again or copy your current database with the existing tables to /tmp.
If it resolves the error message, you can look for a better place than /tmp :-)
run this in command shell:
python manage.py migrate
This fixed for me.
In case it helps anyone else: the problem for me was that I didn't have the django.contrib.sessions app uncommented in my INSTALLED_APPS. Uncommenting it, and rerunning a syncdb did the trick.
In my case the problem was that I forgot to run manage.py syncdb after I made some changes. When I did that the problem was solved.
When I run "manage.py runserver". If I run when I my current path is not in project dir.(such as python /somefolder/somefolder2/currentprj/manage.py runserver) I'll got the problem like you. solve by cd to project directory before run command.
One other possible cause can come from using:
./manage.py testserver
And then visiting the admin interface. This won't work because testserver creates a completely separate database in memory. If you want to visit the admin interface you need to use runserver.
This can happen if there are pending session migrations.
You have 17 unapplied migration(s). Your project may not work properly
until you apply the migrations for app(s): admin, auth, contenttypes,
sessions.
You can use the following command to run the migrations:
python manage.py migrate
This will fix the issue.
It happens may be beacause of undone migrations
• Executes following commands:
python manage.py showmigrations
python manage.py migrate --fake your_app_name zero
python manage.py showmigrations
• IF you are running on local machine, then remove file named 0001.init.py from migrations folder in your app
• Executes following commands:
python manage.py makemigrations
python manage.py migrate
• then run django server:
python manage.py runserver
had the same issue, my resolution was to simply add 'django.contrib.comments' to INSTALLED_APPS and run ./manage.py syncdb again.
I had similar problem for admin management. After several checks, run "python manage.py migrate" without assign APP's name (get "Apply all migrations: .....), then runserver and up on the web. It worked. I hope this could help.
You have unapplied migrations. your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
python manage.py migrate This one worked for me.
Maybe you have some unmigrated files.
Run
Python manage.py makemigrations appname
python manage.py migrate appname
python manage.py runserver
But if the error still continue
The run python manage.py migrate
The run server
create a schema and add its name under NAME in 'databases'
run manage.py syncdb
I had made some changes in Model which was not migrated to db properly. Using the command
manage.py makemigrations
fixed my problem. I hope this will help someone.
syncdb is obsolete try python manage.py makemigrations and python manage.py migrate solved the problem ,and donot forget to add the app name is the installed app in settings.py
I had the same problem so I ran
heroku run ls
and found that the db.sqlite3 file was missing from the server.
In my case, it was because I exempted it by adding it in .gitignore file.
Maybe it is not getting the path of db. Just add this to your settings.py:
import os
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
#modify your db NAME as below:
'NAME': os.path.join(PROJECT_PATH,'mysite.sqlite3'),
In my case I only focused on different project and did not migrate the main application.
so first:
manage.py makemigrations
manage.py migrate
and then to the project's:
manage.py makemigrations <my other project name>
manage.py migrate <my other project name>
Had this problem too. Restarting postgres and apache2 did it for me. Makes me wonder if there was some kind of sqlite process left over which wasn't removed until you fiddled with the files or something.
I had this issue in a different scenario. I am new to Django and cloned a repository from github to practice on it. The file db.sqlite3 was also copied. But there was no django_session in it. When I did
./manage.py showmigrations
.. I found out that there were some migrations. But the tables were missing in sqlite, as I never ran migrate. My issue was resolved when I ran the migrate command. Hope this helps django newbies like me.
./manage.py migrate
Add 'django.contrib.sessions', line in INSTALLED_APPS
Run below commands from django shell
python manage.py makemigrations #check for changes
python manage.py migrate #apply changes in DbSQLite
python manage.py syncdb #sync with database
django_session will appear in database with (session_key, session_data , expire_date)
In my case, I had to erase the 'sessions' entry in the django_migrations table and run makemigrations and migrate afterwards. That created the django_session table.
For me, it was that I updated settings.py, ran the migrations, but the systemd process was still using SQLite because I did not reload it. Doing systemctl restart service_name solved the problem.
And it may be a case you are getting this error because you forget to run query python manage.py migrate before creating super user
If you are tired of using makemigrations and migrate but the error is same no such table django_session. Then just have a look to you code somewhere or the other you using session or calling it. Just comment the code where you are using session and then run the command makemigrations and migrate respectively. It 100% solve your issue.The cause of this error is that you deleted your migrations folder and database file that is the reason you are getting this error.Feel free to ask if problem is not solve
This worked for me.
From https://docs.djangoproject.com/en/2.2/topics/http/sessions/
Using database-backed sessions -
If you want to use a database-backed session, you need to add 'django.contrib.sessions' to your INSTALLED_APPS setting.
Once you have configured your installation, run manage.py migrate to install the single database table that stores session data.
Please try the following command when you change on your code
manage.py makemigrations
manage.py migrate
Simply run this commands
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
it's simple just run the following command
python ./manage.py migrate
python ./manage.py makemigrations AppName

When should you use django-admin.py versus manage.py?

Background:
When I run the django-admin.py loaddata example.json I get this error. "ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined." I understand the problem. It needs the DJANGO_SETTINGS_MODULE to be able to access the database to do this import. I've had this problem before and I've managed to side step it thus far.
In reading the docs, I discovered that the manage.py is a wrapper for django-admin.py; it puts the project on the sys.path and sets the DJANGO_SETTINGS_MODULE environment. Woot! Whoa! I know how to fix my problem.
Soo...
Why do the Django documentation code examples use django-admin.py instead of manage.py when demonstrating subcommands such as loaddata and dumpdata?
If your DJANGO_SETTINGS_MODULE environment variable is set, you can use django-admin.py from any working directory, whereas you need to be in the project directory to use ./manage.py (or have it in your path).
Use virtualenv, and have DJANGO_SETTINGS_MODULE set by bin/activate, and then you can use django-admin.py.
Why do the Django documentation code examples using django-admin.py instead of manage.py when demonstrating subcommands such as loaddata and dumpdata?
Well, because these scripts are the same in priciple, with the differences, you already mentioned. The Django docs also mention
django-admin.py <subcommand> [options]
manage.py <subcommand> [options]
side by side. Usually you use django-admin.py to start a new project or application and manage.py to do the rest.