【问题标题】:.py not reading from the content folder.py 不从内容文件夹中读取
【发布时间】:2019-08-03 09:04:23
【问题描述】:
|-content 
  |-utils
    |- parse_config.py
  |-models.py

folder structure 这是我在 google colab 中的文件夹结构。

我已经安装了 Pytorch 和该项目的所有其他要求。 这里models.py 文件无法访问utils 文件夹中的文件。

在我的models.py 中,我正在导入位于 utils 文件夹中的utils.parse_config,但它显示以下错误。

ImportErrorTraceback (most recent call last) <ipython-input-6-77f4a3369184> in <module>()
----> 1 from models import *
      2 from utils import *
      3 
      4 import os, sys, time, datetime, random
      5 import torch

/content/models.py in <module>()
      9 from PIL import Image
     10 
---> 11 from utils.parse_config import *
     12 from utils.utils import build_targets
     13 from collections import defaultdict

ImportError: No module named utils.parse_config

--------------------------------------------------------------------------- NOTE: If your import is failing due to a missing package, you can manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the "Open Examples" button below.
---------------------------------------------------------------------------

Error

如何让 models.py 访问 utils 文件夹中的文件?

【问题讨论】:

  • 如果您想获得好的响应或任何响应,您必须包含所有相关代码。

标签: python python-3.x deep-learning pytorch google-colaboratory


【解决方案1】:

我看到的是您正在尝试从 utils.parse_config 和 utils 脚本中导入所有内容。 从 utils.parse_config,做

import *

因为它与 Models.py 不在同一个文件夹中,所以做

from ..utils.parse_config import *

你可以尝试弄乱你的python路径,这样比较简单:

import sys
sys.path.append("enter the path")
from utils.parse_config import *

虽然通常不建议这样做。

【讨论】:

    【解决方案2】:

    Models.py 似乎不在 utils 文件夹中。请重新检查

    【讨论】:

    • 我知道 models.py 不在 utils 文件夹中,但我需要访问 utils 文件夹中的文件,请查看错误
    猜你喜欢
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 2017-04-19
    • 2017-08-13
    • 1970-01-01
    • 2012-01-29
    相关资源
    最近更新 更多