简体   繁体   中英

Import error when importing python file from same directory

I have the identical problem to the question posed here: Django custom form ImportError even though file is in the same directory

This is from my urls.py in a django application:

import bulkEdit
...
...
urlpatters = patterns('',
    url(r'^engine/$', component_views.engine, name='engine'),
    ...
    url(r'^admin/', include(bulkEdit.urls)),

My bulkEdit.py file is in the same directory as urls.py.

The error I get is

  File "/home/context/work/riot/src/venv/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
ImportError: No module named bulkEdit

I get the same error if I try

from bulkEdit import urls as bulkEditUrls
...
...
urlpatters = patterns('',
    url(r'^engine/$', component_views.engine, name='engine'),
    ...
    url(r'^admin/', include(bulkEditUrls)),

bulkEdit is a file in the same directory as my urls.py file; file structure is

Rapier
|-component
|    |-__init__.py
|    |-admin.py
|    |-forms.py
|    |-models.py
|    |-views.py
|
|-Chassis
|    |-__init__.py
|    |-urls.py
|    |-bulkEdit.py
|    |-settings.py
|    |-views.py
|
|-manage.py

here is what I have tried so far (In all of these cases, 'Chassis' is in INSTALLED_APPS ):

Using Python 2.7, so I get a syntax error with

import .bulkEdit

I've also tried:

url(r'^admin/', include(Chassis.bulkEdit.urls)),

gives me NameError: name 'Chassis' is not defined

url(r'^admin/', include("Chassis.bulkEdit.urls")),

gives me ImportError: No module named urls

url(r'^admin/', include("Chassis.bulkEdit")),

gives me ImproperlyConfigured: The included urlconf <module 'Chassis.bulkEdit' from '/home/userag/work/project/src/project/Chassis/bulkEdit.pyc'> doesn't have any patterns in it

url(r'^admin/', include(Chassis.bulkEdit)),

gives me NameError: name 'Chassis' is not defined

When I have

import bulkEdit
...
test = url(r'^admin/', include(bulkEdit.urls))

I get no error as long as it is not in the urlpatterns. When I add test to urlpatterns

urlpatterns = patterns('',
    url(r'^engine/$', component_views.engine, name='engine'),
    ...
    test

I get the error. Is there somewhere else I need to import bulkEdit due to me doing things with admin forms?

尝试相对导入:

from .bulkEdit import urls as bulkEditUrls

UPDATED : After comments

try: url(r'^admin/', include("Chassis.bulkEdit.urls"))

but first create the bulkEdit package and in there put the urls.py files. Then copy the code in bulkEdit.py to the

__init__.py 

file of the package in order to provide backward compatibility with other parts of your code.

The problem may has to do with the fact that bulkEdit.urls isn't a python module.

your dir structure after changes:

Rapier
|-component
|    |-__init__.py
|    |-admin.py
|    |-forms.py
|    |-models.py
|    |-views.py
|
|-Chassis
|    |-__init__.py
|    |-urls.py
|    |-bulkEdit
|        |-__init__.py     
|        |-urls.py     
|    |-settings.py
|    |-views.py
|
|-manage.py

The error was in my actual url pattern in bulkEdit/urls.py

urlpatterns = patterns('',
    (r'(?P<app_name>[^/])/(?P<model_name>[^/]+)-masschange/(?P<object_ids>[0-9,]+)/$', 'bulkEdit.mass_change_view'),)

should have been

urlpatterns = patterns('',
    (r'(?P<app_name>[^/])/(?P<model_name>[^/]+)-masschange/(?P<object_ids>[0-9,]+)/$', 'Chassis.bulkEdit.bulkEdit.mass_change_view'),)

The error of No module named bulkEdit originated from here.

But django actually imports your bulkEdit from other directory, and I suppose bulkEdit containing folder is not in sys.path (other option is absence of __init__.py ), hence the error. As your application usually is in sys.path , you can try to specify

url(r'^admin/', include('Chassis.bulkEdit.urls'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM