代码之家  ›  专栏  ›  技术社区  ›  WJA

已安装自定义包,但在另一个项目中找不到

  •  1
  • WJA  · 技术社区  · 6 年前

    /modulename/__init__.py
    /modulename/setup.py
    /modulename/somefunctions.py
    /modulename/README.md
    

    我的包裹的名字是 module_helloworld setup.py 情况如下:

    import setuptools
    
    with open("README.md", "r") as fh:
        long_description = fh.read()
    
    setuptools.setup(
        name="module_helloworld",
        version="0.0.1",
        author="Hello World",
        author_email="hello@world.com",
        description="Hello world module",
        long_description=long_description,
        long_description_content_type="text/markdown",
        url="https://www.website.com",
        packages=setuptools.find_packages(),
        classifiers=[
            "Programming Language :: Python :: 3",
            "License :: OSI Approved :: MIT License",
            "Operating System :: OS Independent",
        ],
    )
    

    在另一个项目中 ,我使用命令在Pycharm中安装了它

    pip install git+https://github.com/Username/module-helloworld.git
    

    这很好,在我的项目设置中,我看到安装了该软件包( 注意到它是以名称安装的 module-helloworld 然而 ) .

    现在,当我打开python控制台(或新的python文件)并键入

    import module_helloworld
    

    然后我得到一个错误:

    ModuleNotFoundError: No module named 'module_helloworld'
    

    我做错了什么?

    1 回复  |  直到 6 年前
        1
  •  0
  •   WJA    6 年前

    在我的情况下,我必须按如下方式重新构造文件夹结构:

    root/module_helloworld/__init__.py
    root/module_helloworld/somefunctions.py
    root/setup.py
    

    然后在另一个项目中,我可以用正常的方式称之为。

    函数内部 __init__.py 要导入函数,我必须将其更改为以下内容:

    # import somefunctions changed to:
    from module_helloworld import somefunctions