manage.py - ImportError: No module named django

38,243

Solution 1

Since you just migrated to a UNIX environment, i suggest you migrate also to the best practices on such a platform too.

  1. Download PIP

    sudo apt-get install python-pip

  2. Download and install virtualenv to set up a separate python virtual environment for your apps. This will allow you to run different flavors of django and other software without conflicts.

    sudo pip install virtualenv

  3. Create virtual environment by running. You will get a folder called myvirtualenvironment with a bin folder and a few executables inside it.

    virtualenv myvirtualenvironment --no-site-packages

  4. In order to tell your shell that you're working with that newly created virtual environment you need to run the activate script found in /myvirtualenvironment/bin/

    source myvirtualenvironment/bin/activate

  5. Now you can install django specifically to that virtual environment.

    pip install django OR pip install django==1.6 depending on what version you want to install. If you don't specify, the latest version will be installed.

  6. Now, migrate your Django project inside of /myvirtualenvironment/ and run the the runserver command.

Solution 2

Sometimes there are some .pyc files in the directories and you don't get any error from the console. Trying to install Django from pip.

sudo pip install django

Best practices advise to create a requirements.txt file (From you Windows installation)

pip freeze > requirements.txt

And then create a new virutalenv to install every package

mkvirtualenv  myapp
pip install -r requirements.txt 
Share:
38,243
Tobias
Author by

Tobias

Updated on December 05, 2020

Comments

  • Tobias
    Tobias over 3 years

    I just ported a working django app from a windows system to ubuntu by just copying all the files to /var/www/some/dir/djangoApp. But now, when executing

    python manage.py runserver 8080
    

    I get the Error:

    ImportError: no module named django
    

    I have already installed a fresh version of django with python setup.py install to /usr/local/lib/python2.7/dist-packages/django/ and added the path to PYTHONPATH.

    The linux system in not maintained by me and has numerous python versions installed.

    calling >>> import django in the shell does not raise an ImportError.

    I'm very confused. Please help me!

    Here's the traceback from the console:

    Traceback (most recent call last):
      File "manage.py", line 13, in <module>
        execute_from_command_line(sys.argv)
      File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
        utility.execute()
      File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 242, in run_from_argv
        self.execute(*args, **options.__dict__)
      File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 280, in execute
        translation.activate('en-us')
      File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 130, in activate
        return _trans.activate(language)
      File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 188, in activate
        _active.value = translation(language)
      File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 177, in translation
        default_translation = _fetch(settings.LANGUAGE_CODE)
      File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 159, in _fetch
        app = import_module(appname)
      File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 40, in import_module
        __import__(name)
    ImportError: No module named django
    
    • petkostas
      petkostas almost 10 years
      You need to install all the packages, setup a virtual env, then in the original machine: pip freeze > requirements.txt Get the requirements.txt from the original machine to your sandbox and then: In your virtualenv: pip install -r requirements.txt
  • Tobias
    Tobias almost 10 years
    Hi. thank you very much. I combined your answer with the one from user669003 and now it works.
  • Tobias
    Tobias almost 10 years
    Hi. thank you very much. I combined your answer with the virtualenv solution provided by asaji. now it works
  • asaji
    asaji almost 10 years
    No problem! Don't forget to accept an answer- if you don't people will be unlikely to answer your future questions