简体   繁体   English

导入模块时出现基本 python 错误 - 获取 modulenotfound 错误

[英]Basic python error with importing module - getting modulenotfound error

I'm doing something basic with python, and I'm getting a pretty common error, but not able to find exactly what's wrong.我正在使用 python 做一些基本的事情,我遇到了一个很常见的错误,但无法准确找到问题所在。 I'm trying to use a custom module (built by someone else).我正在尝试使用自定义模块(由其他人构建)。 I have the folder structure like this:我有这样的文件夹结构:

在此处输入图像描述

There is the test folder, and I have a file testing.py within that:test文件夹,其中有一个文件testing.py

在此处输入图像描述

The contents of testing.py is: testing.py的内容是:

from util import get_data, plot_data
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

When I run this file, using python testing.py, I get this:当我运行这个文件时,使用 python testing.py,我得到这个:

在此处输入图像描述

I went through the other questions that speak about paths, and this looks fine, so not sure what I am missing here.我经历了其他关于路径的问题,这看起来不错,所以不确定我在这里遗漏了什么。 My environment is setup using conda, and the environment is active.我的环境是使用 conda 设置的,并且环境是活动的。

EDIT编辑

As per @allan-wind, I made the relative edit, which got me past the error, but now getting different errors:根据@allan-wind,我进行了相对编辑,这让我克服了错误,但现在出现了不同的错误:

I tried the relative import, and it got past that error, but then it is now throwing this error:我尝试了相对导入,它克服了那个错误,但现在它抛出了这个错误:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\envs\ml4t\lib\multiprocessing\context.py", line 190, in get_context
    ctx = _concrete_contexts[method]
KeyError: 'fork'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "grade_analysis.py", line 21, in <module>
    from grading.grading import (

  File "E:\_Repo\GT\CS7646\mls4tsp23\grading\grading.py", line 15, in <module>
    multiprocessing.set_start_method('fork')

  File "C:\ProgramData\Anaconda3\envs\ml4t\lib\multiprocessing\context.py", line 246, in set_start_method
    self._actual_context = self.get_context(method)
  File "C:\ProgramData\Anaconda3\envs\ml4t\lib\multiprocessing\context.py", line 238, in get_context
    return super().get_context(method)
  File "C:\ProgramData\Anaconda3\envs\ml4t\lib\multiprocessing\context.py", line 192, in get_context
    raise ValueError('cannot find context for %r' % method)
ValueError: cannot find context for 'fork'

` `

There are a number of ways to specify how to find modules:有多种方法可以指定如何查找模块:

  1. Use a relative import :使用相对导入
from .util import get_data, plot_data
  1. Set the environment variable PYTHONPATH includes the directory where you module resides.设置环境变量 PYTHONPATH 包括你的模块所在的目录。

  2. See sys.meta_path参见sys.meta_path

Seems like the script was meant to be run from a different directory.似乎该脚本应该从不同的目录运行。 People don't usually name a top level module utils .人们通常不会将顶级模块命名为utils You could try finding a subdirectory of the custom module with a utils dir and running the script from there.您可以尝试使用utils目录查找自定义模块的子目录并从那里运行脚本。

On another note, if this is all the content the script has, it is probably of no real significance and can be casually ignored.另一方面,如果这就是脚本的全部内容,那么它可能没有任何实际意义,可以随便忽略。 The import is never used in the code.导入从未在代码中使用。 The code so far seems to basically do nothing.到目前为止的代码似乎基本上什么都不做。

Just place utils in the same folder as your testing.py and the python interpreter you put that directory in your path.只需将 utils 放在与您的 testing.py 和 python 解释器相同的文件夹中,您将该目录放在您的路径中。 Other solutions would be to place utils in a directory that is already in your path, since if thats not the case, you cant import from "above" the current directory其他解决方案是将 utils 放在路径中已经存在的目录中,因为如果不是这样,您将无法从当前目录“上方”导入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM