简体   繁体   中英

Python homemade package import error after pip install

If I make a very simple package like this (with empty __init__.py files):

package_name/
    package_name/
         sub_module/
              __init__.py
              file.py  
         __init__.py
    setup.py

When I start a Python shell at the root of this project, and do:

import package_name.sub_module.file

everything goes well and I can use the functions in file.py . But if I send this project on a GitLab, and pip install it in another local project and import it the same way, I get the following error:

ModuleNotFoundError: No module named 'package_name.sub_module'

I tried to modify the __init__.py files in many ways but I can't find something working. I don't understand why this happen.

EDIT: Here is the content of the setup.py file:

from setuptools import setup

setup(name='package_name',
      version='0.1',
      description='My package',
      url='https://gitlab.myserver.com/package_name',
      author='Me',
      author_email='me@myserver.com',
      license='MIT',
      packages=['package_name'],
      install_requires=[
          'another_package_1',
          'another_package_2'
      ],
      zip_safe=False)

To install the package with pip, I use the command:

pip install git+https://gitlab.myserver.com/package_name.git

look on this site https://docs.python.org/3/distutils/setupscript.html
you have to declare every subfolder in the project structure

packages=['an_example_pypi_project', 'tests'],

|-- an_example_pypi_project
|   |-- __init__.py
|   |-- useful_1.py
|   |-- useful_2.py
|-- tests
|-- |-- __init__.py
|-- |-- runall.py
|-- |-- test0.py

|-- an_example_pypi_project
|   |-- __init__.py
|   |-- useful_1.py
    |-- subfolder
|   |   |-- useful_2.py
|-- tests
|-- |-- __init__.py
|-- |-- runall.py
|-- |-- test0.py

packages=['an_example_pypi_project', an_example_pypi_project.subfolder, 'tests'],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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