python 中的导入错误
ImportError in python
在clean.py我有:
import datetime
import os
from flask_script import Manager
from sqlalchemy_utils import dependent_objects
from components import db, app
from modules.general.models import File
from modules.workflow import Workflow
manager = Manager(usage='Cleanup manager')
@manager.command
def run(dryrun=False):
for abandoned_workflow in Workflow.query.filter(Workflow.current_endpoint == "upload.upload_init"):
if abandoned_workflow.started + datetime.timedelta(hours=12) < datetime.datetime.utcnow():
print("Removing abandoned workflow {0} in project {1}".format(
abandoned_workflow.id, abandoned_workflow.project.name
))
if not dryrun:
db.session.delete(abandoned_workflow)
db.session.commit()
for file in File.query.all():
dependencies_number = dependent_objects(file).count()
print("File {0} at {1} has {2} dependencies".format(file.name, file.path, dependencies_number))
if not dependencies_number:
file_delete(file, dryrun)
if not dryrun:
db.session.delete(file)
db.session.commit()
# List all files in FILE_STORAGE directory and delete ones tat don't have records in DB
all_files_hash = list(zip(*db.session.query(File.hash).all()))
for file in os.listdir(app.config['FILE_STORAGE']):
if file.endswith('.dat'):
continue
if file not in all_files_hash:
file_delete(os.path.join(app.config['FILE_STORAGE'], file), dryrun)enter code here
我需要开始 def 运行()
在控制台我写:
python clean.py
我有输出:
`Traceback (most recent call last):
File "cleanup_command.py", line 7, in <module>
from components import db, app
ImportError: No module named 'components'
clean.py 位于- C:\App\model\clean.py
components.py 位于 - C:\components.py
Workflow.py 位于 - C:\modules\workflow\Workflow.py
请告诉我可能是什么问题?
问题是在某些位置搜索要导入的模块:https://docs.python.org/2/tutorial/modules.html#the-module-search-path。
在您的情况下,您可以将所有源目录路径放在 PYTHONPATH
var 中,例如:
PYTHONPATH=... python clean.py
但我想最好重新定位您的代码文件(即将所有库放在一个位置)
要在调用 python clean.py
时启动 运行(),请在脚本末尾添加这些行。
if __name__ == '__main__':
r = run()
## 0-127 is a safe return range, and 1 is a standard default error
if r < 0 or r > 127: r = 1
sys.exit(r)
正如 Eugene Primako 提到的,最好将您的代码文件重新定位在一个位置。
from components import db, app
ImportError: No module named 'components'
这意味着它在 clean.py 所在的位置寻找名为 components.py 的脚本。这就是您出现导入错误的原因。
在clean.py我有:
import datetime
import os
from flask_script import Manager
from sqlalchemy_utils import dependent_objects
from components import db, app
from modules.general.models import File
from modules.workflow import Workflow
manager = Manager(usage='Cleanup manager')
@manager.command
def run(dryrun=False):
for abandoned_workflow in Workflow.query.filter(Workflow.current_endpoint == "upload.upload_init"):
if abandoned_workflow.started + datetime.timedelta(hours=12) < datetime.datetime.utcnow():
print("Removing abandoned workflow {0} in project {1}".format(
abandoned_workflow.id, abandoned_workflow.project.name
))
if not dryrun:
db.session.delete(abandoned_workflow)
db.session.commit()
for file in File.query.all():
dependencies_number = dependent_objects(file).count()
print("File {0} at {1} has {2} dependencies".format(file.name, file.path, dependencies_number))
if not dependencies_number:
file_delete(file, dryrun)
if not dryrun:
db.session.delete(file)
db.session.commit()
# List all files in FILE_STORAGE directory and delete ones tat don't have records in DB
all_files_hash = list(zip(*db.session.query(File.hash).all()))
for file in os.listdir(app.config['FILE_STORAGE']):
if file.endswith('.dat'):
continue
if file not in all_files_hash:
file_delete(os.path.join(app.config['FILE_STORAGE'], file), dryrun)enter code here
我需要开始 def 运行()
在控制台我写:
python clean.py
我有输出:
`Traceback (most recent call last):
File "cleanup_command.py", line 7, in <module>
from components import db, app
ImportError: No module named 'components'
clean.py 位于- C:\App\model\clean.py
components.py 位于 - C:\components.py
Workflow.py 位于 - C:\modules\workflow\Workflow.py
请告诉我可能是什么问题?
问题是在某些位置搜索要导入的模块:https://docs.python.org/2/tutorial/modules.html#the-module-search-path。
在您的情况下,您可以将所有源目录路径放在 PYTHONPATH
var 中,例如:
PYTHONPATH=... python clean.py
但我想最好重新定位您的代码文件(即将所有库放在一个位置)
要在调用 python clean.py
时启动 运行(),请在脚本末尾添加这些行。
if __name__ == '__main__':
r = run()
## 0-127 is a safe return range, and 1 is a standard default error
if r < 0 or r > 127: r = 1
sys.exit(r)
正如 Eugene Primako 提到的,最好将您的代码文件重新定位在一个位置。
from components import db, app
ImportError: No module named 'components'
这意味着它在 clean.py 所在的位置寻找名为 components.py 的脚本。这就是您出现导入错误的原因。