ImportError:没有名为mysite.settings的模块(Django)

时间:2016-03-24 21:56:58

标签: python django

我在ubuntu 15.10上安装了Djangomod_wsgi-express。基本上(注意我做了以root身份执行此操作):

~/.local/bin $ ./mod_wsgi-express start-server ~/mysite/mysite/wsgi.py

接下来我做了:

~/mysite/mysite/wsgi.py

其中:[Thu Mar 24 22:26:24.638043 2016] [wsgi:error] [pid 19469:tid 139785018738560] mod = importlib.import_module(self.SETTINGS_MODULE) [Thu Mar 24 22:26:24.638070 2016] [wsgi:error] [pid 19469:tid 139785018738560] File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module [Thu Mar 24 22:26:24.781030 2016] [wsgi:error] [pid 19469:tid 139785018738560] __import__(name) [Thu Mar 24 22:26:24.781148 2016] [wsgi:error] [pid 19469:tid 139785018738560] ImportError: No module named mysite.settings [Thu Mar 24 22:26:27.590300 2016] [wsgi:error] [pid 19469:tid 139784895194880] [remote 92.243.236.53:24636] mod_wsgi (pid=19469): Target WSGI script '/tmp/mod_wsgi-localhost:8000:1000/handler.wsgi' cannot be loaded as Python module. 来自我上传到服务器上方目的地的sample project。但是当我尝试访问网站时出现错误(内部服务器错误)。当我查看日志时,我看到:

os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings" 

所以似乎无法找到 mysite.settings /不在Python PATH上(文件〜/ mysite / mysite / settings.py 确实存在)。

基于: https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/

我试图添加:

import sys
#Wrong!
#sys.path.append("/home/user/mysite/mysite")

#Correct
sys.path.append("/home/user/mysite")

但它没有帮助。我还尝试将以上路径添加到示例项目到python路径: https://code.djangoproject.com/wiki/PythonPath

但同样的错误。我错过了什么?

修改/溶液

问题出在包含路径中:

public saveScaledPhotoToFile() {
    //Convert your photo to a bitmap
    Bitmap photoBm = (Bitmap) "your Bitmap image";
    //get its orginal dimensions
    int bmOriginalWidth = photoBm.getWidth();
    int bmOriginalHeight = photoBm.getHeight();
    double originalWidthToHeightRatio =  1.0 * bmOriginalWidth / bmOriginalHeight;
    double originalHeightToWidthRatio =  1.0 * bmOriginalHeight / bmOriginalWidth;
    //choose a maximum height
    int maxHeight = 1024;
    //choose a max width
    int maxWidth = 1024;
    //call the method to get the scaled bitmap
    photoBm = getScaledBitmap(photoBm, bmOriginalWidth, bmOriginalHeight,
            originalWidthToHeightRatio, originalHeightToWidthRatio,
            maxHeight, maxWidth);

    /**********THE REST OF THIS IS FROM Prabu's answer*******/
    //create a byte array output stream to hold the photo's bytes
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    //compress the photo's bytes into the byte array output stream
    photoBm.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

    //construct a File object to save the scaled file to
    File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "Imagename.jpg");
    //create the file
    f.createNewFile();

    //create an FileOutputStream on the created file
    FileOutputStream fo = new FileOutputStream(f);
    //write the photo's bytes to the file
    fo.write(bytes.toByteArray());

    //finish by closing the FileOutputStream
    fo.close();
}

private static Bitmap getScaledBitmap(Bitmap bm, int bmOriginalWidth, int bmOriginalHeight, double originalWidthToHeightRatio, double originalHeightToWidthRatio, int maxHeight, int maxWidth) {
    if(bmOriginalWidth > maxWidth || bmOriginalHeight > maxHeight) {
        Log.v(TAG, format("RESIZING bitmap FROM %sx%s ", bmOriginalWidth, bmOriginalHeight));

        if(bmOriginalWidth > bmOriginalHeight) {
            bm = scaleDeminsFromWidth(bm, maxWidth, bmOriginalHeight, originalHeightToWidthRatio);
        } else {
            bm = scaleDeminsFromHeight(bm, maxHeight, bmOriginalHeight, originalWidthToHeightRatio);
        }

        Log.v(TAG, format("RESIZED bitmap TO %sx%s ", bm.getWidth(), bm.getHeight()));
    }
    return bm;
}

private static Bitmap scaleDeminsFromHeight(Bitmap bm, int maxHeight, int bmOriginalHeight, double originalWidthToHeightRatio) {
    int newHeight = (int) Math.min(maxHeight, bmOriginalHeight * .55);
    int newWidth = (int) (newHeight * originalWidthToHeightRatio);
    bm = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
    return bm;
}

private static Bitmap scaleDeminsFromWidth(Bitmap bm, int maxWidth, int bmOriginalWidth, double originalHeightToWidthRatio) {
    //scale the width
    int newWidth = (int) Math.min(maxWidth, bmOriginalWidth * .75);
    int newHeight = (int) (newWidth * originalHeightToWidthRatio);
    bm = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true);
    return bm;
}

5 个答案:

答案 0 :(得分:18)

将此添加到 wsgi.py 文件

path = '/home/path/to/project'
if path not in sys.path:
    sys.path.append(path)
设置前

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

答案 1 :(得分:10)

请尝试:


    import os
    import sys
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.append(BASE_DIR)
    os.environ['DJANGO_SETTINGS_MODULE'] = 'YOURAPP.settings'
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "YOURAPP.settings")

这适用于主项目目录中的脚本..

答案 2 :(得分:6)

问题出在包含路径中:

import sys
#Wrong!
#sys.path.append("/home/user/mysite/mysite")

#Correct
sys.path.append("/home/user/mysite")

答案 3 :(得分:1)

当我尝试将Windows项目移动到Debian时,我也遇到了这个错误。

import sys
sys.path.append("your project path")

答案 4 :(得分:0)

此外,如果您使用的是Visual Studio,请检查Django的应用程序属性是否与您期望的设置模块匹配。默认情况下,它设置为$(MSBuildProjectName).settings,这对我来说是一个问题,因为我的项目名称和应用名称不一样。

您可以通过在解决方案资源管理器中右键单击您的应用并点击"属性"来找到此设置。然后是左边的Django标签。