Unable to import module

I created a python web application with this directory structure:

# cd /usr/local/www/myapp modules layout __init__.py layout.py packages public myapp.wsgi 

I installed PYTHONPATH for:

 /usr/local/www/myapp/modules:/usr/local/www/myapp/packages 

In myapp.wsgi I am trying to do:

 import layout 

But I get an internal server error. Why?

This is my myapp.wsgi (if I delete the import layout line, it works):

 import sys import wsgiref import layout def application(environ, start_response): response_status = '200 OK' response_body = 'Hello! ' response_headers = [] content_type = ('Content-type', 'text-plain') content_length = ('Content-Length', str(len(response_body))) response_headers.append(content_type) response_headers.append(content_length) start_response(response_status, response_headers) return [response_body] 

The full error message that I get:

 Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, webmaster@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log. 

My virtual host configuration:

 <VirtualHost *:80> ServerName localhost ServerAlias localhost ServerAdmin webmaster@example.com DocumentRoot /usr/local/www/myapp/public <Directory /usr/local/www/myapp/public> Order allow,deny Allow from all </Directory> WSGIScriptAlias / /usr/local/www/myapp/myapp.wsgi <Directory /usr/local/www/myapp> Order allow,deny Allow from all </Directory> </VirtualHost> 

Error from / var / log / httpd -error.log:

 [Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] mod_wsgi (pid=1725): Target WSGI script '/usr/local/www/myapp/myapp.wsgi' cannot be loaded as Python module. [Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] mod_wsgi (pid=1725): Exception occurred processing WSGI script '/usr/local/www/myapp/myapp.wsgi'. [Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] Traceback (most recent call last): [Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] File "/usr/local/www/myapp/myapp.wsgi", line 3, in <module> [Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] import layout [Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] ImportError: No module named layout 

Print output sys.path:

enter image description here

+6
source share
5 answers

First try:

 python /usr/local/www/myapp/myapp.wsgi 

Is it loading correctly?

If so, then maybe you have an environment (in ~/.bashrc or such) that is necessary for your application. Try ::

 # to wipe-out extra env env -i bash # try again python /usr/local/www/myapp/myapp.wsgi 

Make sure you use the same python in your shell as the one used by apache WSGI.

If your myapp.wsgi needs an extra env to load properly, you can do one of the following:

  • set python path to apache or
  • install at runtime in myapp.wsgi

To install the WSGI code, here is a sample code.

 import os, sys EXTRA_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..')) if EXTRA_DIR not in sys.path: sys.path.append(EXTRA_DIR) 

Insert the file myapp.wsgi at the top of the file.

+8
source

You have __init.__py in your layout folder, but it should be __init__.py . The period is inappropriate. I am not sure if this is a typo in your message or not, but if it looks like your file, it will cause this problem.

+1
source
Directory Directory

also needs the __init__.py file, which will be defined as a package.

0
source

I had a similar problem and this solved it:

 chmod a+x myapp.wsgi 
0
source

According to http://webpy.org/install#apachemodwsgi

If you get "ImportError: No module named web" in the apache error.log file, you can try setting the absolute path to code.py before importing web:

 import sys, os abspath = os.path.dirname(__file__) sys.path.append(abspath) os.chdir(abspath) import web 

This works for me significantly.

0
source

Source: https://habr.com/ru/post/906490/


All Articles