0

I'm sure this is an extremely simple question, but I cannot seem to figure it out. I'm getting into python and have use a basic 'hello world' router as an example. I wanted to export the majority of the code to expand the app into multiple files. When all of the code is in a single file, it works fine, but when it is moved into 2 separate files, it bombs. Any help is greatly appreciated.

FIRST FILE

import re # first file
from Primer.Core.router import Router // second file, excluded when all code is in 1 file

# second file code here when combined

def application(environ, start_response):
    router = Router(environ, start_response)
    return router.run()

SECOND FILE

from cgi import escape


class Router:

def __init__(self, environ, start_response):
    self.environ = environ
    self.start_response = start_response

def run(self):
    path = self.environ.get('PATH_INFO', '').lstrip('/')
    if (path == 'hello')
        return 'hello'
    else
        return 'other'
alex-phillips
  • 838
  • 3
  • 14
  • 24
  • Can you explain what you mean when you say your code "bombs"? What happens? – kirbyfan64sos Apr 27 '14 at 01:56
  • Yup, sorry. I'm running the code in a web browser (via apache) and it returns a 500 error. – alex-phillips Apr 27 '14 at 01:57
  • 1
    Did you read the Apache logs to find out what's going on? – Crowman Apr 27 '14 at 01:58
  • Can you check the error log? See [this](http://stackoverflow.com/questions/4731364/internal-error-500-apache-but-nothing-in-the-logs) for more info. – kirbyfan64sos Apr 27 '14 at 01:59
  • I've check the error, log, it says 'No module named Primer.Core.router'. Here's my file structure: index.py is running, then I have ./Primer/Core/router.py. I have __init__.py files in the Primer AND Core directory – alex-phillips Apr 27 '14 at 02:01
  • 1
    Turns out the issue was I did not have my WSGIPythonPath set in my apache config. I'm still getting used to all this. Thanks for all of the help! – alex-phillips Apr 27 '14 at 04:27

2 Answers2

1

If the two files are in the same directory, you should remove the prefix from the import.

from router import Router

Also, you need to create an empty file __init__.py in the directory that contains the file that will be imported.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
1

For now, I see several errors in the code:

  1. The import will fail as a non-Python comment (//) is written after it. You should use # instead.
  2. You should check that the structure of your files is correct:

    ├── Primer │   ├── Core │   │   ├── __init__.py │   │   └── router.py │   ├── __init__.py └── index.py

  3. The if and else in your Router class lack both a colon:

    def run(self): path = self.environ.get('PATH_INFO', '').lstrip('/') if (path == 'hello'): return 'hello' else: return 'other' Also, the indentation of the methods within your Router class is not properly set on your example, but I suppose it is OK in your file...

I'd check that the errors are not due to that first.

Kiddo
  • 158
  • 1
  • 4
  • 12
  • Sorry for the crudeness of the paste, the // comment is not in my actual code (it was meant only for this post). I've double checked the colon and that is not the issue. – alex-phillips Apr 27 '14 at 02:07
  • Do you have the index.py file and the Primer folder at the same level? Are you sure the import error comes from the index.py file and not from any other file in another subfolder (which might not be able to locate the router.py file)? With the structure I said before it does not raise any error for me, but of course I have only the code you posted. Maybe if you provide some more code or details we could replicate your problem... – Kiddo Apr 27 '14 at 02:19