Nathan Whitney
Nathan Whitney

Reputation: 1

ImportError: No module named django, sys.path fine

I'm trying to configure Django with

python manage.py shell

And I get the error:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/usr/lib64/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/lib64/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/lib64/python2.7/site-packages/django/apps/config.py", line 123, in create
    import_module(entry)
  File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named django

I've seen a lot of questions about this error, and most of them seem to be centered on the fact that django's path is not included in python's sys.path. Mine is, however. When I run

sudo pip install django

I get

Requirement already satisfied: django in /usr/lib64/python2.7/site-packages

And the output for my sys.path is

['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']

So the path is absolutely there. What are my options here? This is a remote interpreter using SSH from Pycharm, and it's seriously delaying my work.

Upvotes: 0

Views: 143

Answers (1)

Antonis Christofides
Antonis Christofides

Reputation: 6939

If which python, when run by the normal user, returns ~/bin/python, then this is a different Python from the one run by the superuser, which, according to what you say, is /bin/python. sudo pip install django runs as the superuser and installs Django in the system's Python environment. When, afterwards, you execute python manage.py shell, it runs another Python, and that other Python's environment apparently doesn't have Django installed.

If you run pip install django without the sudo it might work, but since you have superuser permission on that machine it would be better to get rid of the Python installation that you have in the normal user's home directory, and make sure your system has only one Python installation until you really know what you are doing. Otherwise you will be confused whether you use virtualenv or not.

Edit: I disagree with many comments that tell you to use virtualenv. You are confused enough without it. Let's simplify the problem first. Remove all virtualenvs you have created (you do this by deleting the directories) and forget everything about virtualenv. Logout and login again (this will ensure the virtualenvs are deactivated). Don't modify your manage.py, it's fine as it was originally. Install Django system-wide with sudo pip install django, then try to run your Django project with python manage.py shell. Only after you get it working and you start having a grip on the system go on to start playing with virtualenv. Virtualenv is great, but everything in its time. (virtualenv demystified is an introduction to virtualenv written by me.)

Upvotes: 1

Related Questions