WebFaction
Community site: login faq
0
1

I'm using the Webfaction provided install of Django 1.6.1 / WSGI 3.4 / Python 3.3, my directory structure looks like this:

/home/me/webapps/project/project/
    __init__.py
    settings.py
    wsgi.py
    urls.py
    app/
        __init__.py
        urls.py
        views.py

So the root urlconf points some requests to the app's urlconf. The app's urlconf in turn imports some views from views.py.

However, the import statement fails! At first I thought it might be a sys.path issue, but I cannot recreate the issue in any environment other than the internet-facing http request. After screwing around I found that any import that is not fully qualified fails, an example:

in urls.py:

import project.app.views # this works

import views # this fails

I am at a complete loss, I've never seen a module fail to import a same-directory module before.

It does not seem entirely relevant but I tried adding '' to sys.path, it didn't change anything.

asked 05 Feb '14, 18:12

gfiliotis
3114
accept rate: 0%

It could be a redundant import or something like that (could be another module with the same name in the PythonPath). In any case it is always better to be more explicit so the full qualification is recommended.

(06 Feb '14, 04:54) waynek

views is not defined - I checked

But, Python supports redundant imports by default. Even so, my intention is to use from views import thing but being able to import views is a prerequisite of this.

I doubt 'it is always better' to fully qualify import paths, the project is not a dependency of the app, and really the name of the app is not a dependency of a same-directory import, so it strikes me as silly to have to fully qualify the module path.

(06 Feb '14, 09:12) gfiliotis

Where did you attempt to add the directory to sys.path? Was it in the apache httpd.conf file or in the wsgi file? If it was in the wsgi file or elsewhere in python try setting it in httpd.conf using the complete path to the parent directory.

(07 Feb '14, 01:56) johns

The path was added in httpd.conf, but I tried other places too.

Anyway, the issue is solved now, dhobbs solution does what I originally wanted to, but I was also able to configure the path variable so that I could import with package locality:

from app import module instead of from project.app import module

My research tells me that this is actually the way the python devs originally intended imports to behave.

(10 Feb '14, 22:21) gfiliotis

Python 3 requires the dot-syntax for package-relative imports, like this:

from .views import thing

A single dot represents the current level, two dots is one level up, etc. Otherwise, you need to import the module using the full path from the top package level.

See Removed Syntax from the Python 3.0 What's New, and PEP 328.

permanent link

answered 07 Feb '14, 14:56

dhobbs
2825
accept rate: 100%

Thanks! I actually read the page on the new import behaviors in Py3K and it didn't get through my head that this is what they meant!

(10 Feb '14, 22:23) gfiliotis
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×27
×15
×1
×1

question asked: 05 Feb '14, 18:12

question was seen: 11,551 times

last updated: 10 Feb '14, 22:23

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM