Skip to content
This repository has been archived by the owner on Nov 19, 2023. It is now read-only.

Unable to use the "django-admin.py startapp" command #16

Open
davgar opened this issue Sep 6, 2016 · 6 comments
Open

Unable to use the "django-admin.py startapp" command #16

davgar opened this issue Sep 6, 2016 · 6 comments
Assignees

Comments

@davgar
Copy link

davgar commented Sep 6, 2016

Hello,

I am trying to install the django rest framework and the first thing to do is to create an app. I run the command "django-admin.py startapp api" and here is the output I get:

(virtualenv)[vagrant@gcd-django-box www (example ✗)]$ django-admin.py startapp api

Traceback (most recent call last):

File "/opt/virtualenv/bin/django-admin.py", line 5, in management.execute_from_command_line()

File "/opt/virtualenv/local/lib/python2.7/site-packages/django/core/management/init.py", line 385, in execute_from_command_line utility.execute()

File "/opt/virtualenv/local/lib/python2.7/site-packages/django/core/management/init.py", line 345, in execute settings.INSTALLED_APPS

File "/opt/virtualenv/local/lib/python2.7/site-packages/django/conf/init.py", line 46, in getattr self._setup(name)

File "/opt/virtualenv/local/lib/python2.7/site-packages/django/conf/init.py", line 42, in _setup
self._wrapped = Settings(settings_module)

File "/opt/virtualenv/local/lib/python2.7/site-packages/django/conf/init.py", line 98, in init % (self.SETTINGS_MODULE, e)

ImportError: Could not import settings 'settings' (Is it on sys.path? Is there an import error in the settings file?): No module named settings

I tried to follow this suggestion but it did not work.

I created a separate environment with Django 1.10 and ran the command django-admin.py startproject test and got the following results:

(env)[vagrant@gcd-django-box djangotest ]$ django-admin.py startproject test .

Traceback (most recent call last):
.....
File "/home/vagrant/djangotest/env/local/lib/python2.7/site-packages/django/conf/init.py", line 97, in init mod = importlib.import_module(self.SETTINGS_MODULE)

File "/usr/lib/python2.7/importlib/init.py", line 37, in import_module import(name)

ImportError: No module named settings

Any suggestions greatly appreciated ;)

Thank you,
David

@handrews
Copy link
Contributor

handrews commented Sep 6, 2016

Does it work if you set environment variables on the command line?

PYTHONPATH=/vagrant/www DJANGO_SETTINGS_MODULE=settings django-admin.py startapp

Also, does manage.py work? Without setting envs on the command line?

@davgar
Copy link
Author

davgar commented Sep 6, 2016

I am finally able to create the app :), manage.py does not work without envs.

(virtualenv)[vagrant@gcd-django-box www (example ✗)]$ manage.py
zsh: command not found: manage.py

@handrews
Copy link
Contributor

handrews commented Sep 6, 2016

I don't think manage.py is chmod'd to be directly executable- I always run "python manage.py ". Maybe it should be?

I'll have to try building a new environment to see if I can reproduce this. I've never had the problem with manage.py, but never tried using django-admin.py so that's probably a day one setup bug.

@handrews
Copy link
Contributor

handrews commented Sep 7, 2016

OK I brought up a new environment without doing any customization of settings. I was not able to run django-admin.py, but I am able to run manage.py just fine:

# lots of make install messages ending with...
status gcd-django | grep -q start || sudo start gcd-django
gcd-django start/running, process 23588
(virtualenv)[vagrant@gcd-django-box /vagrant (master)]$ cd www
(virtualenv)[vagrant@gcd-django-box www (master)]$ git branch
* master
(virtualenv)[vagrant@gcd-django-box www (master)]$ which django-admin.py
/opt/virtualenv/bin/django-admin.py
(virtualenv)[vagrant@gcd-django-box www (master)]$ django-admin.py --help
Traceback (most recent call last):
  File "/opt/virtualenv/bin/django-admin.py", line 5, in <module>
    management.execute_from_command_line()
  File "/opt/virtualenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/opt/virtualenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    settings.INSTALLED_APPS
  File "/opt/virtualenv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
    self._setup(name)
  File "/opt/virtualenv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/opt/virtualenv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 98, in __init__
    % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'settings' (Is it on sys.path? Is there an import error in the settings file?): No module named settings
(virtualenv)[vagrant@gcd-django-box www (master)]$ python manage.py shell
Python 2.7.3 (default, Jun 22 2015, 19:43:34) 
Type "copyright", "credits" or "license" for more information.

IPython 0.13.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: 

@handrews
Copy link
Contributor

handrews commented Sep 7, 2016

OK, the problem is that PYTHONPATH needs to be set to get django-admin.py to work:

(virtualenv)[vagrant@gcd-django-box www (master)]$ PYTHONPATH=. django-admin.py --help
Usage: django-admin.py subcommand [options] [args]

Options:
  -v VERBOSITY, --verbosity=VERBOSITY
                        Verbosity level; 0=minimal output, 1=normal output,
# etc. with a whole bunch of help output

I'm pretty sure that mange.py works because it is in the directory that needs to be in PYTHONPATH.

I'll look into the right way to configure this in vagrant (or the stuff run by the Makefile).

@handrews
Copy link
Contributor

handrews commented Sep 8, 2016

Reading up on PYTHONPATH and PYTHONHOME (because I can never remember the difference), we just want to set PYTHONPATH=/vagrant/www to allow any django-aware tool to work.

I also verified that manage.py works because it is in that directory and is passed to the python executable as an argument. From the PYTHONPATH docs: "If a script argument is given, the directory containing the script is inserted in the path in front of $PYTHONPATH."

It should be straightforward to get the vagrant environment to set this up.

@handrews handrews self-assigned this Sep 8, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants