pip install -e成功,导入失败:ModuleNotFoundError [英] pip install -e successful, import fails: ModuleNotFoundError

查看:383
本文介绍了pip install -e成功,导入失败:ModuleNotFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试安装正在开发的模块.当我使用'pip install -e.'时,它会输出'已成功安装的Gym-mabifish',但是当我尝试使用'import gym_mabifish'导入模块时,我得到的是'ModuleNotFoundError:No module named"gym_mabifish"'.

这是包装的结构:

gym-mabifish/
     setup.py ( https://pastebin.com/1wNykyKw )
     gym_mabifish/
          __init__.py ( https://pastebin.com/GtQid3Nk )
          envs/
               __init__.py ( https://pastebin.com/Txfk0ezE )
               mabifish_env.py ( https://pastebin.com/g50zBbus )

我正在使用OpenAI健身房中的random_agent进行测试. ( https://pastebin.com/72LETtxd )

该软件包显示在点列表中:

gym-mabifish (0.0.1, x:\path\to\project\gym-mabifish)

解决方案

您很可能有两个(或更多)Python安装,并且pippython来自不同的安装.

理想情况下,您想解决该问题.但是在某些情况下,这非常困难.在Windows上,管理PATH可能是一场噩梦.在macOS上,如果您在Apple预先安装的Python 2.7(不附带pip)的同时安装第二个Python 2.7,则会使您感到困惑.

因此,当今官方推荐的解决方案是使用-m标志可使用您通常用于运行Python的任何命令来运行pip .

以下所有内容截至2018年初.在过去的几年中,它已经发生了几次更改,并且将来可能会再次发生更改,因此,始终最好直接转到 Windows :

  • py
  • * nix :
    • python仅当您明确使用操作系统/发行版随附的任何内容作为默认设置时.
    • python3(如果在* nix上使用3.x)( https://www.python.org/dev/peps/pep-0394/)
    • python2.7(如果您在macOS上使用Apple的默认Python 2.7).
    • python2,如果您正在* nix上使用其他任何2.x.
  • 因此,例如,在Windows上,应该为:

    py -m pip install gym-mabifish
    

    …,然后像这样运行脚本时:

    py myscript.py
    

    ...它将能够import gym_mabifish.

    I'm trying to install a module I'm developing. When I use 'pip install -e .', it outputs 'Successfully installed gym-mabifish' but when I try to import the module using 'import gym_mabifish' I'm getting 'ModuleNotFoundError: No module named "gym_mabifish"'.

    Here's the structure of the package:

    gym-mabifish/
         setup.py ( https://pastebin.com/1wNykyKw )
         gym_mabifish/
              __init__.py ( https://pastebin.com/GtQid3Nk )
              envs/
                   __init__.py ( https://pastebin.com/Txfk0ezE )
                   mabifish_env.py ( https://pastebin.com/g50zBbus )
    

    I'm using the random_agent from OpenAI gym to test it. ( https://pastebin.com/72LETtxd )

    The package is shown in pip list:

    gym-mabifish (0.0.1, x:\path\to\project\gym-mabifish)
    

    解决方案

    Most likely, you have two (or more) Python installations, and your pip and python come from different installations.

    Ideally, you want to solve that problem. But in some cases, it's very difficult. On Windows, managing the PATH can be a nightmare. On macOS, if you install a second Python 2.7 alongside the Apple pre-installed Python 2.7 (which doesn't come with pip), things get confusing.

    So, the officially recommended solution nowadays is to just use the -m flag to run pip using whichever command you normally use to run Python.

    Everything below is as of early 2018. It's changed a few times in the past few years, and may well change again in the future, so it's always best to go right to Installing Python Modules in the docs, or the Python Packaging User Guide. But as a summary:

    Ideally you're using a virtual environment, in which case this is just python. But if not:

    • Windows:
      • py
    • *nix:
      • python only if you're explicitly using whatever came with your OS/distro as a default.
      • python3 if you're using 3.x on *nix](https://www.python.org/dev/peps/pep-0394/)
      • python2.7 if you're using Apple's default Python 2.7 on macOS.
      • python2 if you're using any other 2.x on *nix.

    So, for example, on Windows, it should be:

    py -m pip install gym-mabifish
    

    … and then when you run your script like this:

    py myscript.py
    

    … it will be able to import gym_mabifish.

    这篇关于pip install -e成功,导入失败:ModuleNotFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆