Python ImportError after setup.py

After installing my python project with setup.pyand running it in the terminal, I get the following error:

...
from ui.mainwindow import MainWindow
  File "/usr/local/lib/python2.7/dist-packages/EpiPy-0.1-py2.7.egg/epipy/ui/mainwindow.py", line 9, in <module>
    from model.sir import SIR
ImportError: No module named model.sir

...

Suppose we have the following structure for our project cookies:

.
β”œβ”€β”€ setup.py
└── src
    β”œβ”€β”€ a
    β”‚   β”œβ”€β”€ aa.py
    β”‚   └── __init__.py
    β”œβ”€β”€ b
    β”‚   β”œβ”€β”€ bb.py
    β”‚   └── __init__.py
    β”œβ”€β”€ __init__.py
    └── main.py

File: cookies/src/main.py

from a import aa

def main():
    print aa.get_aa()

File cookies/src/a/aa.py

from b import bb

def get_aa():
    return bb.get_bb()

File: cookies/src/b/bb.py

def get_bb():
    return 'bb'

File: cookies/setup.py

#!/usr/bin/env python

import os
import sys

try:
    from setuptools import setup, find_packages
except ImportError:
    raise ImportError("Install setup tools")

setup(
    name = "cookies",
    version = "0.1",
    author = "sam",
    description = ("test"),
    license = "MIT",
    keywords = "test",
    url = "asd@ads.asd",
    packages=find_packages(),
    classifiers=[
    """\
    Development Status :: 3 - Alpha
    Operating System :: Unix
    """
    ],
    entry_points = {'console_scripts': ['cookies = src.main:main',],},
)

If I set cookiesboth rootwith $ python setup.py installand perform cookies, I get the following error: ImportError: No module named b. How can I solve the problem.

+4
source share
2 answers

I would like to use absolute imports everywhere (from epipypsy imports ...). This is what is recommended in PEP 328 .

, . PYTHONPATH, , , , 'editable ': pip install -e

python . , , .

:

/home/jbchouinard/mypackage. , . from mypackage import subpackage.

pip install, , /usr/lib/python 2.7/dist-packages. , / . .

pip install -e, /usr/lib/python 2.7/dist-packages (.pth ) /home/jbchouinard/mypackage. import mypackage, , /home/jbchouinard/mypackage; .

+3

. , ( , sys os, ), (, ):

import sys
import os
sys.path.insert(1, os.path.join(sys.path[0], '..'))
# all other imports go here...

, main.py (epipy). , , : -)

+1

Source: https://habr.com/ru/post/1623479/


All Articles