简体   繁体   English

pip 安装可编辑 Package 产生 ModuleNotFoundError

[英]pip Install Editable Package Produces ModuleNotFoundError

Due to import issues, I've followed the steps shown here to install my Python project as an editable pip package. Basically this entails running pip install -e.由于导入问题,我按照此处显示的步骤将我的 Python 项目安装为可编辑的pip package。基本上这需要运行pip install -e. from my project root directory.从我的项目根目录。 The name of the project is 'myproject', with the setup.py configured as such:项目的名称是“myproject”, setup.py配置如下:

from setuptools import setup, find_packages

setup(name='myproject', version='1.0', packages=find_packages())

The project structure is like so:项目结构是这样的:

.
├── myproject
│   ├── core
│   │   ├── core.py
│   │   └── __init__.py
│   └── tests
│       ├── __init__.py
│       └── test_one.py
├── setup.py
└── env
    └── ...

With the venv activated, I get the following output:激活venv ,我得到以下 output:

(env) [root@localhost /]$ python -V
Python 3.6.3

(env) [root@localhost /]$ pip -V
pip 9.0.1 from /myproject/venv/lib64/python3.6/site-packages (python 3.6)

However, when running an interpreted session I experience the following:但是,在运行解释的 session 时,我遇到以下情况:

(env) [root@localhost /]$ python
>>> import myproject
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'myproject'
>>> from myproject.core import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'myproject'

Starting up another interpreted session and running the setuptools stuff myself results in:启动另一个解释 session 并自己运行setuptools东西会导致:

(env) [root@localhost /]$ python
>>> from setuptools import find_packages
>>> find_packages()
>>> ['core', 'tests']

I've also tried other methods of installing it, including:我还尝试了其他安装方法,包括:

python -m pip install -e .

And still get the same problems.仍然遇到同样的问题。 Finally, I can do the following:最后,我可以执行以下操作:

(env) [root@localhost /] pip list installed | grep myproject
myproject (1.0, /myproject)

UPDATE : As shown here and as mentioned by @Fletchy1995 below, changing the directory structure so that it is instead like:更新:如此处所示以及下面@Fletchy1995 所提到的,更改目录结构,使其类似于:

.
├── setup.py
├── myproject
│   ├── core
│   │   ├── core.py
│   │   └── __init__.py
│   └── tests
│       ├── __init__.py
│       └── test_one.py
├── __init__.py
└── env
    └── ...

And modifying setup.py to look like:并将setup.py修改为:

from setuptools import setup

setup(name='myproject', version='1.0', packages=['myproject'])

along with running pip install -e.连同运行pip install -e. from the top level directory seems to have fixed the problem.从顶级目录似乎已经解决了这个问题。 But in the previous case , even though the packages loaded include all sub-packages of 'myproject', why can I still not do something like:但是在前面的案例中,即使加载的包包括'myproject'的所有子包,为什么我仍然不能做这样的事情:

(env) [root@localhost /]$ python
>>> from myproject.core import *

as 'myproject' is listed in pip ?因为“我的项目”列在pip中?

It may work if you modify the setup.py to如果您将setup.py修改为

from setuptools import setup, find_packages

setup(name='myproject', version='1.0', packages=find_packages(where='myproject'))

Then, you can then run pip install -e from the root directory to install the package in editable mode.然后,您可以从根目录运行pip install -e以可编辑模式安装 package。

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

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