0

This is such a strange thing I am unable to understand it.

This is my django file directory hierarchy in brief

project/
       apache/ django.wsgi
       project/ __init__.py, settings.py, urls.py ..
       services/
               __init__.py
               apis/
                    __init__.py
                    fparser.py
               wservice.py
       ...

       profile.py

So, everything works fine in development server and even on heroku (gunicorn) but isn't working on apache (localhost)

when I was opening the page:

Its showing

Exception Type: ImportError at /
Exception Value: cannot import name website_feed_address

this website_feed_address is located at profile.py the import error was found at fparser.py

how should I fix it?

EDIT:

django.wsgi

import os, sys

sys.path.append('d:/code/projects-dev/project')


os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

while the project dir hierarchy mentioned above in the question is at d:/code/projects-dev/

Edit 2

These are the apache log errors

[Sun Jul 08 23:14:04 2012] [notice] Parent: Received restart signal -- Restarting the server.
httpd.exe: Could not reliably determine the server's fully qualified domain name, using 124.123.136.220 for ServerName
[Sun Jul 08 23:14:04 2012] [warn] mod_wsgi: Compiled for Python/2.7.
[Sun Jul 08 23:14:04 2012] [warn] mod_wsgi: Runtime using Python/2.7.2.
[Sun Jul 08 23:14:05 2012] [notice] Child 5912: Exit event signaled. Child process is ending.
[Sun Jul 08 23:14:05 2012] [warn] RSA server certificate CommonName (CN) `127.0.0.1' does NOT match server name!?
[Sun Jul 08 23:14:05 2012] [notice] Apache/2.2.22 (Win32) mod_wsgi/3.3 Python/2.7.2 mod_ssl/2.2.22 OpenSSL/0.9.8t configured -- resuming normal operations
[Sun Jul 08 23:14:05 2012] [notice] Server built: Jan 28 2012 11:16:39
[Sun Jul 08 23:14:05 2012] [notice] Parent: Created child process 2120
httpd.exe: Could not reliably determine the server's fully qualified domain name, using 124.123.136.220 for ServerName
[Sun Jul 08 23:14:05 2012] [warn] RSA server certificate CommonName (CN) `127.0.0.1' does NOT match server name!?
httpd.exe: Could not reliably determine the server's fully qualified domain name, using 124.123.136.220 for ServerName
[Sun Jul 08 23:14:05 2012] [warn] mod_wsgi: Compiled for Python/2.7.
[Sun Jul 08 23:14:05 2012] [warn] mod_wsgi: Runtime using Python/2.7.2.
[Sun Jul 08 23:14:05 2012] [warn] RSA server certificate CommonName (CN) `127.0.0.1' does NOT match server name!?
[Sun Jul 08 23:14:05 2012] [notice] Child 2120: Child process is running
[Sun Jul 08 23:14:06 2012] [notice] Child 2120: Acquired the start mutex.
[Sun Jul 08 23:14:06 2012] [notice] Child 5912: Released the start mutex
[Sun Jul 08 23:14:06 2012] [notice] Child 2120: Starting 64 worker threads.
[Sun Jul 08 23:14:06 2012] [notice] Child 2120: Starting thread to listen on port 443.
[Sun Jul 08 23:14:06 2012] [notice] Child 2120: Starting thread to listen on port 80.
[Sun Jul 08 23:14:07 2012] [notice] Child 5912: Terminating 126 threads that failed to exit.
[Sun Jul 08 23:14:07 2012] [notice] Child 5912: All worker threads have exited.
[Sun Jul 08 23:14:07 2012] [notice] Child 5912: Child process is exiting

Edit 3

this is how profile.py, fparser.py look

profile.py just contains some variables, tuples just like settings.py. Its just importing a variable website_feed_address in this case.

this is fparser.py

from profile import website_feed_address
import feedparser


class FParser(object):
      def __init__(self):
            self.pFeed = feedparser.parse(website_feed_address)
      # rest of the code goes...

I just want to use website_feed_address in FParser class without taking it as a object argument. Is there any better way to do?.. or else, can I just use it like this?

2
  • How do you define the PYTHONPATH that Apache uses? How do you import website_feed_address? Commented Jul 8, 2012 at 15:34
  • @Rodrigue well as I use Eclipse.. most of the PATH kind of things are taken care by it. regarding the django.wsgi, I've added more details in the above question. pls, check it Commented Jul 8, 2012 at 15:39

2 Answers 2

1

Most likely the issue is that you are using package-relative import statements with in your project, yet Apache has no knowledge of the proper python path. For instance, with this specific import error you are getting, I assume in your fparser.py you are doing something like from profile import website_feed_address. You might try explicitly including your project package in your django.wsgi:

sys.path.append('d:/code/projects-dev/project')
sys.path.append('d:/code/projects-dev/project/project')

An even better recommendation is that you set up a virtualenv for you project. The benefit of doing so will allow your project to contain its own site-packages environment, so that any dependencies you install will live with the project env. Then in Apache you can tell mod_wsgi where the python environment is:

# or wherever you decide to create the virtualenv
WSGIPythonHome d:/code/projects-dev/project/project

Setting the pythonpath via Apache

You can also try setting the PYTHONPATH for your wsgi app in the Apache conf:

WSGIPythonPath "d:/code/projects-dev/project/project"

*Note in that link where it makes the distinction between wsgi embedded mode and daemon mode.

Completely alternative approach to addressing the import issue

While my previous suggestions were addressing the PYTHONPATH and importing your profile module, based on the fact that you are simply using it to pass in a constant, I would suggest you just make use of the settings.py, as thats what it is for (declaring constants):

settings.py

WEBSITE_FEED_ADDRESS = "http://foo.com"

fparser.py

from django.conf import settings

...
    self.pFeed = feedparser.parse(settings.WEBSITE_FEED_ADDRESS)

You can be pretty sure that the django environment will always make the settings module available to you. The common approach is to have apps add to the settings, to provide default constants.

Sign up to request clarification or add additional context in comments.

6 Comments

save the virtual env's for some time. I'd would consider it as last option. I don't know why.. but I just don't like every time to get off with virtual env's..
I think virtualenv is a perfect way to get started. It always ensures that your project has a contained environment, and that you weren't relying on outside packages.
can you look at the question again.. I added some more details
That just confirms that you are relying on profile being in your pythonpath. You might want to change it to from project.profile import website_feed_address if you don't want to fix the PYTHONPATH elsewhere. I just added some extra info as well.
The worst part of everything.. I decided to pass the address directly as string inside the constructor. this time, the page isn't loading... not even showing error. showing "waiting for localhost". what exactly the whole problem would be in my situation?
|
0

If you move profile.py into the project folder it will be inside a package. Then you can do:

from project.profile import website_feed_address

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.