Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
785 views
in Technique[技术] by (71.8m points)

pip - Python package structure not importing correct module

Following the PYPI documentation, it shows how to install a package, which works great. But once I introduce a class to a file I can't located the class. I've attached a lot of code but i believe the context is necessary.

Now this works fine when I'm working in the directory but once its uploaded to PYPI and used elsewhere on my computer it can't find the class.

Tree

package_upload
├── EasyNN
│?? ├── EasyNN.py
│?? ├── __init__.py
│?? └── import_this.py
├── LICENSE.txt
├── README.md
└── setup.py

Setup.py

import setuptools

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()


setuptools.setup(
    name='EasyNN',
    version='0.0.3',
    description='Currently Testing',
    packages=setuptools.find_packages(),
    python_requires='>=3.6',
    url="https://github.com/danielwilczak101/EasyNN",
    author="Daniel",
    author_email="[email protected]",
    long_description = long_description,
    long_description_content_type = "text/markdown",
    classifier=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)",
        "Operating System :: OS Independent",
        ],
    install_requires = ["matplotlib ~= 3.3.2",
                        "pytest>=3.7",
                        "tabulate >=0.8.7"
                        ],
    )

EasyNN.py

import import_this

class NN:

    def say_hello(self):
        print("Hello NN World!")

import_this.py

def foo():
    print("I've been imported")

Running Python Code

import EasyNN
nn = EasyNN.NN()

Error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'EasyNN' has no attribute 'NN'
>>> 
question from:https://stackoverflow.com/questions/65916756/python-package-structure-not-importing-correct-module

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are trying to access NN, which lives inside of EasyNN/EasyNN.py. If you want to make the NN class available from a top-level import, you have to include it in your top-level __init__.py file:

# EasyNN/__init__.py

from EasyNN import NN

Then, from outside of the package, elsewhere on your computer, NN is now available from a top-level import.

When you import a package, you can't directly access the objects contained within the files that live inside of that package. You need to include them in your __init__.py of the respective package, or else access them like this:

from EasyNN.EasyNN import NN

Above, the first EasyNN is the package, but the second one is the .py file called EasyNN that conatins the class NN.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...