Django ImportError No module named x.settings
<p>I have the following structure:</p>

<pre><code>mysite
  -&gt; manage.py
  -&gt; mysite (again)
        -&gt; __init__.py
        -&gt; wsgi.py
        -&gt; settings.py
         etc
  -&gt; myapp
        -&gt; __init__.py            
        -&gt; myscript.py
        -&gt; models.py
        etc
</code></pre>

<p>When I run a script from myapp (that does myapp-related things, putting stuff in a the database for example) i need to do </p>

<pre><code>import django
django.setup()
</code></pre>

<p>in order to be able to <code>from models import MyModel</code>. But if i do this in the myapp directory, I get:</p>

<pre><code>Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "C:\Python27\lib\site-packages\django\__init__.py", line 22, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 53, in __getattr__
    self._setup(name)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 97, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named mysite.settings
</code></pre>

<p>Which I kind of understand since it's further up the directory tree and not in the  same directory as e.g <code>manage.py</code> (the root I guess?). When I issue a python interpreter in mysite where <code>manage.py</code> is located, I dont get this error.</p>

<p>What should I do to be able to place my scripts in myapp and still be able to use <code>django.setup()</code> from that dir? </p>

<p>You need to make sure the root of your project is in python path when you run the script. Something like this might help.</p>

<pre>
import os
import sys
projpath = os.path.dirname(__file__)
sys.path.append(os.path.join(projpath, '..'))
</pre>

python django