|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 2830680393 于 2023-7-17 17:04 编辑
吴恩达机器学习中代码:
import numpy as np
%matplotlib widget
import matplotlib.pyplot as plt
from lab_utils_uni import plt_intuition, plt_stationary, plt_update_onclick, soup_bowl
plt.style.use('./deeplearning.mplstyle')
在运行代码时报错如下:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[8], line 4
2 get_ipython().run_line_magic('matplotlib', 'widget')
3 import matplotlib.pyplot as plt
----> 4 from lab_utils_uni import plt_intuition, plt_stationary, plt_update_onclick, soup_bowl
5 plt.style.use('deeplearning.mplstyle')
File ~\Desktop\2022-Machine-Learning-Specialization-main\Supervised Machine Learning Regression and Classification\week1\4.Regression Model\lab_utils_uni.py:11
9 from matplotlib.colors import LinearSegmentedColormap
10 from ipywidgets import interact
---> 11 from lab_utils_common import compute_cost
12 from lab_utils_common import dlblue, dlorange, dldarkred, dlmagenta, dlpurple, dlcolors
14 plt.style.use('./deeplearning.mplstyle')
File ~\Desktop\2022-Machine-Learning-Specialization-main\Supervised Machine Learning Regression and Classification\week1\4.Regression Model\lab_utils_common.py:9
6 import numpy as np
7 import matplotlib.pyplot as plt
----> 9 plt.style.use('./deeplearning.mplstyle')
10 dlblue = '#0096ff'; dlorange = '#FF9300'; dldarkred='#C00000'; dlmagenta='#FF40FF'; dlpurple='#7030A0';
11 dlcolors = [dlblue, dlorange, dldarkred, dlmagenta, dlpurple]
File D:\tool\anaconda\lib\site-packages\matplotlib\style\core.py:153, in use(style)
151 pkg, _, name = style.rpartition(".")
152 try:
--> 153 path = (importlib_resources.files(pkg)
154 / f"{name}.{STYLE_EXTENSION}")
155 style = _rc_params_in_file(path)
156 except (ModuleNotFoundError, IOError) as exc:
157 # There is an ambiguity whether a dotted name refers to a
158 # package.style_name or to a dotted file path. Currently,
(...)
161 # either use Path objects or be prepended with "./" and use
162 # the slash as marker for file paths.
File D:\tool\anaconda\lib\importlib\_common.py:22, in files(package)
17 def files(package):
18 # type: (Package) -> Traversable
19 """
20 Get a Traversable resource from a package
21 """
---> 22 return from_package(get_package(package))
File D:\tool\anaconda\lib\importlib\_common.py:66, in get_package(package)
60 def get_package(package):
61 # type: (Package) -> types.ModuleType
62 """Take a package name or module object and return the module.
63
64 Raise an exception if the resolved module is not a package.
65 """
---> 66 resolved = resolve(package)
67 if wrap_spec(resolved).submodule_search_locations is None:
68 raise TypeError(f'{package!r} is not a package')
File D:\tool\anaconda\lib\importlib\_common.py:57, in resolve(cand)
55 def resolve(cand):
56 # type: (Package) -> types.ModuleType
---> 57 return cand if isinstance(cand, types.ModuleType) else importlib.import_module(cand)
File D:\tool\anaconda\lib\importlib\__init__.py:121, in import_module(name, package)
118 if not package:
119 msg = ("the 'package' argument is required to perform a relative "
120 "import for {!r}")
--> 121 raise TypeError(msg.format(name))
122 for character in name:
123 if character != '.':
TypeError: the 'package' argument is required to perform a relative import for './deeplearning'
请问怎么解决啊?
根据我从网上搜索到的信息,这个错误是由于您使用了相对导入的方式,但没有指定包名。相对导入需要使用`package`参数来指定相对于哪个包进行导入。
在您的代码中,您使用了`from .upload.urls import ...`这样的语句,但没有告诉Python`.upload.urls`是相对于哪个包的。这可能会导致Python无法找到正确的模块路径,从而抛出`TypeError`异常。
为了解决这个问题,您可以尝试以下几种方法:
- 使用绝对导入的方式,即不要在模块名前加点,而是直接写出完整的模块名,例如`from upload.urls import ...`。
- 在相对导入的语句中,添加`package`参数,指定当前模块所属的包名,例如`from .upload.urls import ..., package='courseworkupload'`。
- 确保您的项目目录中有一个`__init__.py`文件,以便Python将其识别为一个包。
|
|