zjm1126
zjm1126

Reputation: 66677

how to import the blog.py(i import the 'blog' folder)

my dir location,i am in a.py:

my_Project
     |----blog
            |-----__init__.py
            |-----a.py
            |-----blog.py

when i 'from blog import something' in a.py , it show error:

from blog import BaseRequestHandler
ImportError: cannot import name BaseRequestHandler

i think it import the blog folder,not the blog.py

so how to import the blog.py

updated

when i use 'blog.blog', it show this:

from blog.blog import BaseRequestHandler
ImportError: No module named blog

updated2

my sys.path is :

['D:\\zjm_code', 'D:\\Python25\\lib\\site-packages\\setuptools-0.6c11-py2.5.egg', 'D:\\Python25\\lib\\site-packages\\whoosh-0.3.18-py2.5.egg', 'C:\\WINDOWS\\system32\\python25.zip', 'D:\\Python25\\DLLs', 'D:\\Python25\\lib', 'D:\\Python25\\lib\\plat-win', 'D:\\Python25\\lib\\lib-tk', 'D:\\Python25', 'D:\\Python25\\lib\\site-packages', 'D:\\Python25\\lib\\site-packages\\PIL']


zjm_code
    |-----a.py
    |-----b.py

a.py is :

c="ccc"

b.py is :

from a import c
print c

and when i execute b.py ,i show this:

> "D:\Python25\pythonw.exe"  "D:\zjm_code\b.py" 
Traceback (most recent call last):
  File "D:\zjm_code\b.py", line 2, in <module>
    from a  import c
ImportError: cannot import name c

Upvotes: 1

Views: 281

Answers (2)

Auston
Auston

Reputation: 478

what happens when you:

import blog

Try outputting your sys.path, in order to make sure that you have the right dir to call the module from.

Upvotes: 0

Olivier Verdier
Olivier Verdier

Reputation: 49166

When you are in a.py, import blog should import the local blog.py and nothing else. Quoting the docs:

modules are searched in the list of directories given by the variable sys.path which is initialized from the directory containing the input script

So my guess is that somehow, the name BaseRequestHandler is not defined in the file blog.py.

Upvotes: 1

Related Questions