如何导入博客.py(我导入“blog”文件夹)

2024-04-20 13:26:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我的dir位置,我在a.py中:

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

当我在a.py中“from blog import something”时,它显示错误:

^{pr2}$

我认为它导入的是博客文件夹,而不是博客.py在

那么如何导入博客.py在

更新

当我使用'博客.blog,它显示了:

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

已更新2

我的系统路径是:

['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是:

c="ccc"

b.py是:

from a import c
print c

当我执行b.py时,我会显示:

> "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

Tags: frompyimporteggmylibpackagesdir
2条回答

当你:

import blog

尝试输出搜索路径,以确保有正确的dir从中调用模块。在

当您在a.py中时,import blog应该导入本地blog.py,而不导入其他内容。引用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

因此,我的猜测是,文件BaseRequestHandler中没有定义名称BaseRequestHandler。在

相关问题 更多 >