问题描述
所以我收到一个FileNotFoundError: [Errno 2] No such file or directory error
,但实际上我不知道该怎么办,因为该文件肯定存在。当我尝试在命令heroku run "otree resetdb"
之后使用heroku和git部署oTree实验时,发生错误。操作系统是Windows 10。
完整错误消息:
Running otree resetdb on ⬢ otreeexperimentna... up,run.5607 (Free)
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.7/site-packages/otree_startup/__init__.py",line 227,in do_django_setup
django.setup()
File "/app/.heroku/python/lib/python3.7/site-packages/django/__init__.py",line 24,in setup
apps.populate(settings.INSTALLED_APPS)
File "/app/.heroku/python/lib/python3.7/site-packages/django/apps/registry.py",line 114,in populate
app_config.import_models()
File "/app/.heroku/python/lib/python3.7/site-packages/django/apps/config.py",line 211,in import_models
self.models_module = import_module(models_module_name)
File "/app/.heroku/python/lib/python3.7/importlib/__init__.py",line 127,in import_module
return _bootstrap._gcd_import(name[level:],package,level)
File "<frozen importlib._bootstrap>",line 1006,in _gcd_import
File "<frozen importlib._bootstrap>",line 983,in _find_and_load
File "<frozen importlib._bootstrap>",line 967,in _find_and_load_unlocked
File "<frozen importlib._bootstrap>",line 677,in _load_unlocked
File "<frozen importlib._bootstrap_external>",line 728,in exec_module
File "<frozen importlib._bootstrap>",line 219,in _call_with_frames_removed
File "/app/experimentMusic/models.py",line 21,in <module>
class Constants(BaseConstants):
File "/app/experimentMusic/models.py",line 26,in Constants
with open('D:\Experiment\oTreeproject\oTreeonlineshop-master\shop\oTree\experimentMusic\products.json','r',encoding='utf-8') as jsonfile:
FileNotFoundError: [Errno 2] No such file or directory: 'D:\\Experiment\\oTreeproject\\oTreeonlineshop-master\\shop\\oTree\\experimentMusic\\products.json'
我还尝试了带有正常斜杠和双反斜杠的代码版本,但仍然收到相同的错误。接下来,我尝试使用pathlib中的Path重写models.py
中的路径,如下所示:
from pathlib import Path
with open(Path('D:/Experiment/oTreeproject/oTreeonlineshop-master/shop/oTree/experimentMusic/products.json'),encoding='utf-8') as jsonfile:
data=jsonfile.read()
shoppinglist = json.loads(data)
可能是什么问题?文件products.json存在并且不为空,因此我不知道为什么它不起作用。我还尝试在笔记本电脑上运行该实验,并且效果很好,所以我猜问题出在heroku上。欢迎任何建议,非常感谢!
最好, 丽娜
解决方法
我知道了。解决方案是将.json
文件的路径重写为相对路径(而不是绝对路径)。