简体   繁体   English

pip install -e成功,导入失败:ModuleNotFoundError

[英]pip install -e successful, import fails: ModuleNotFoundError

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"'. 当我使用'pip install -e。'时,它输出'成功安装的Gym-mabifish',但是当我尝试使用'import gym_mabifish'导入模块时,我得到的是'ModuleNotFoundError:没有名为“ 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. 我正在使用来自OpenAI Gym的random_agent进行测试。 ( https://pastebin.com/72LETtxd ) 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. 很可能您有两个(或更多)Python安装,而pippython来自不同的安装。

Ideally, you want to solve that problem. 理想情况下,您要解决该问题。 But in some cases, it's very difficult. 但是在某些情况下,这非常困难。 On Windows, managing the PATH can be a nightmare. 在Windows上,管理PATH可能是一场噩梦。 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. 在macOS上,如果您在Apple预先安装的Python 2.7(不带pip )上安装了第二个Python 2.7,则会使您感到困惑。

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 . 因此,当今官方推荐的解决方案是仅使用-m标志使用您通常用于运行Python的任何命令来运行pip

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 . 以下所有内容截至2018年初。在过去几年中已进行了几次更改,并且将来可能会再次更改,因此,始终最好直接在docs或Python Packaging User Guide中 安装Python模块 But as a summary: 但作为总结:

Ideally you're using a virtual environment, in which case this is just python . 理想情况下,您使用的是虚拟环境,在这种情况下,这只是python But if not: 但是,如果没有:

  • Windows : Windows
    • py
  • *nix : * nix
    • python only if you're explicitly using whatever came with your OS/distro as a default. 仅当您明确使用OS /发行版随附的任何内容作为默认选项时,才使用python
    • python3 if you're using 3.x on *nix]( https://www.python.org/dev/peps/pep-0394/ ) 如果您在* nix上使用3.x,则为python3 ]( https://www.python.org/dev/peps/pep-0394/
    • python2.7 if you're using Apple's default Python 2.7 on macOS. python2.7如果您在macOS上使用Apple的默认Python 2.7)。
    • python2 if you're using any other 2.x on *nix. 如果在* nix上使用任何其他2.x,则为python2

So, for example, on Windows, it should be: 因此,例如,在Windows上,应为:

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 . …它将能够import gym_mabifish

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

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