SoFunction
Updated on 2025-04-23

Solution to the problem of failure to import Python from wsgi

On a apache2/flask server, there is a folder structure as follows:

/var/www/myapp
/var/www/myapp/
/var/www/myapp/__init__.py
/var/www/wsgi-scripts/

The application file () in the myapp folder is as follows:

from flask import Flask, render_template
app = Flask(__name__)

@('/')
def welkom():
    return render_template('')


if __name__ == '__main__':
     = 'True'
    ()

The files in wsgi-scripts are as follows:

import sys
(0, '/var/www/myapp')
from myapp import routing as application

But when I load the page I get the following error in the log:

[Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23574): Target WSGI script '/var/www/wsgi-scripts/' cannot be loaded as Python module.
[Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23574): Exception occurred processing WSGI script '/var/www/wsgi-scripts/'.
[Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] Traceback (most recent call last):
[Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11]   File "/var/www/wsgi-scripts/", line 3, in <module>
[Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11]     import  as application
[Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] ImportError: No module named 
[Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23575): Target WSGI script '/var/www/wsgi-scripts/' cannot be loaded as Python module.
[Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23575): Exception occurred processing WSGI script '/var/www/wsgi-scripts/'.
[Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] Traceback (most recent call last):
[Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11]   File "/var/www/wsgi-scripts/", line 3, in <module>
[Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11]     import  as application
[Fri Mar 15 08:06:32 2013] [error] [client 192.168.1.11] ImportError: No module named 
[Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23577): Target WSGI script '/var/www/wsgi-scripts/' cannot be loaded as Python module.
[Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23577): Exception occurred processing WSGI script '/var/www/wsgi-scripts/'.
[Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] Traceback (most recent call last):
[Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11]   File "/var/www/wsgi-scripts/", line 3, in <module>
[Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11]     import  as application
[Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] ImportError: No module named 
[Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23573): Target WSGI script '/var/www/wsgi-scripts/' cannot be loaded as Python module.
[Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] mod_wsgi (pid=23573): Exception occurred processing WSGI script '/var/www/wsgi-scripts/'.
[Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] Traceback (most recent call last):
[Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11]   File "/var/www/wsgi-scripts/", line 3, in <module>
[Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11]     import  as application
[Fri Mar 15 08:06:33 2013] [error] [client 192.168.1.11] ImportError: No module named 

Solution

The reason for the problem is that Python import from wsgi failed, which may be due to the following reasons:

  • The module is not imported correctly.
  • The module is not correctly defined.

Method 1:

The solution given in the first answer is:

  • Import the routing module directly, not the module.
  • Define application as a function, not a module.

Code example:

# 
import sys
(0, '/var/www/myapp')

from routing import app as application
# 
def app(environ, start_response):
     = 'True'
    ()

Method 2:

The solution given in the second answer is:

  • Use the() function to add the myapp directory to Python's package path.
  • Use [:] = new_sys_path to place the new path in front of Python's package path.

Code example:

# 
ALLDIRS = ['/var/www/myapp/']

import sys 
import site 

# Remember original .
prev_sys_path = list() 

# Add each new site-packages directory.
for directory in ALLDIRS:
  (directory)

# Reorder  so new directories at the front.
new_sys_path = [] 
for item in list(): 
    if item not in prev_sys_path: 
        new_sys_path.append(item) 
        (item) 
sys.

This is the end of this article about the problem of failing to import Python from wsgi. For more related content related to the failure to import Python from wsgi, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!