AI健身房环境:尽管所有步骤都正确,但没有模块发现错误



我确信我遵循了正确的步骤在AI Gym中注册了我的自定义环境。但是当一个__init__.py文件无法识别文件夹并且没有发现模块错误时,我会遇到一个问题我通过OneDrive使用Anaconda Jupyterlab,这样它就可以同步,我可以在任何设备上工作。

路径是C->用户->myname->OneDrive->我的代码->gym_mycode

  1. gym_mycode-->envs文件夹和第一个__init__.py文件
  2. 内部环境-->另一个__init__.py、custom_env和其他一些文件

我的第一个__ init__的内容是:

from gym.envs.registration import register
register(id="PyABCD-v0", entry_point="gym_mycode.envs:CustomEnv_class")

我在envs文件夹中的第二个__init__的内容是:

from gym_mycode.envs.custom_env import CustomEnv_class

第二个给了我一个名为"gym_mycode"的错误No模块。它怎么可能无法识别gym_mycode文件夹?是因为我在OneDrive而不是某个特定于蟒蛇的文件夹中操作这整件事吗?

我首先在envs中运行第一个init,然后运行第二个init。希望这个运行顺序是正确的。

编辑根据要求,下面是当前目录和回溯。

os.getcwd()C:UsersHPOneDriveMy_codegym_mycodeenvs

ModuleNotFoundError                       Traceback (most recent call last)
Input In [23], in <cell line: 6>()
3 import sys
4 sys.path.append('C:\Users\HP\OneDrive\My_code\gym_mycode')
----> 6 from gym_mycode.envs.custom_env import CustomEnv_class
ModuleNotFoundError: No module named 'gym_mycode'

代码可能在不同的文件夹中运行(current working directory-检查os.getcwd()(,然后import搜索模块gym_mycode可能在不同文件夹中运行。

您可能需要在import gym_mycode之前将文件夹/full/path/to/My_code添加到sys.path

常见的错误是添加文件的路径,但必须是文件夹的路径。

import sys
sys.path.append("/full/path/to/My_code")      # add at the end of list
#sys.path.insert(0, "/full/path/to/My_code")  # add at the beginning of list
import gym_mycode

最新更新