manage.py runserver - ImportError: No module named MYSITE - django

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.

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

Can't find .env file for locally starting Django app using 'heroku local'

I want to local run my Django app with Heroku using, for example, 'heroku local -e .env.test' (see https://devcenter.heroku.com/articles/heroku-local). I am using virtualenvwrapper so my envs (test, dev) are not in my Django project server but located in my $WORKON_HOME directory. I don't know what to specify for the last part of the command because I can't find the .env files in the $WORKON_HOME.
I've tried heroku local -e $WORKON_HOME/dev and heroku local -e $VIRTUAL_ENV and get the same error: ▸ EISDIR: EISDIR: illegal operation on a directory, read
For me, the problem was that I had created a virtualenv directory called .env, conflicting with Heroku's ENV system which uses the same filename. Deleting the virtualenv and recreating it as .venv solved my problem:
deactivate
rm -rf .env
virtualenv .venv
source .venv/bin/activate
NB. You can't just rename the .env directory without having to manually edit the virtualenv configuration too; better just to destroy and recreate.
I think the confusion comes from what you think .env does: that is used by the Procfile (which is called by Foreman https://ddollar.github.io/foreman/ ) to set environment variables like:
A=b
C=d
.env there is not your virtualenv, which you should not git track.
Just use your virtualenv as usual before calling heroku local and track a requirements.txt (and possibly runtime.txt for the Python version, see https://devcenter.heroku.com/articles/deploying-python )
Heroku will automatically use virtualenv when you push.
As usual, look for minimal working examples to get started: https://github.com/heroku/heroku-django-template
Answering my own question:
I was able to solve my issue by issuing the following command from the base directory of my Django project.
echo "source /home/your_username/.virtualenvs/venv_name_here/bin/activate" >> .env
[the command is referenced here: How to create/make your app LOCAL with Heroku/Virtualenv/Django? ]
Reiterating, this was needed because virtualenvwrapper doesn't automatically create .env's. Thus,heroku local's apparent need for a .env requires a manual creation of the environmental file. #ciro-santilli-巴拿馬文件-六四事件-法轮功 if you know a better way, such as some option of heroku local, I'd like to know.

how to start a new django project from any directory

I'm running a windows 8 machine. I've installed django using pip and everything looks to be done correctly. running python 2.7 and have django 1.6 installed.
python c:\python27\scripts\django-admin.py startproject mysite
this is the only way I've found to start a new project from any directory; otherwise I would have to make my project and run it from that specific folder. I want to be able to just:
python django-admin.py startproject mysite
from any directory - or more simply:
django-admin.py startproject mysite
from anywhere. I have:
c:\python27\;c:\python\scripts\
in my environment variables.
Not a windows guy, but try running the command from the directory where you want your site to live.
cd C:\Sites
python django-admin.py startproject mysite
If that doesn't work, then is that you don't have Django in your PATH. Add Django to your PATH. Otherwise you'll have to do
python C:\path\to\django-admin.py startproject mysite
Also check out https://docs.djangoproject.com/en/dev/intro/install/ for more documentation on setting up your Django environment.
Hope that helps!
You could create a .cmd file that contains
python c:\python27\scripts\django-admin.py %1 %2 %3 %4 %5
name it django-admin.cmd and put it in c:\windows\system32
Then you should be able to
django-admin startproject mysite anywhere
it seems like the problem is the .py files's default open way.
first, find a .py file.
right click on it,and select "open with" , here u should use python as your default open mode
then restart your cmd, it should be worked.
here is another same problem Can't open file 'django-admin.py': [Errno 2] No such file or directory
You can use virtualenv's. After creating an env, installing django, and activating and env django-admin commands should become available. A recent similar question.

Django local install on ubuntu

I am trying to install django locally on ubuntu but cannot get it to recognize admin-django.py. I performed the subversion checkout, moved the django directory on to the python path and then created a symlink to django-admin.py. I can import django from within the python interpreter but cannot run
django-admin.py startproject mysite
Any ideas what I could be missing?
You probably need to give the full path to django-admin.py. Giving us the error message would help.
If your symlink is in the current directory (you don't say) then:
./django-admin.py
should work. Otherwise its
/usr/wherever/python/site-packages/something/django/huh/django-admin.py
Make sure that the django-admin.py file is executable:
chmod +x django-admin.py

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.