为什么我的 Python 包将文件直接安装到 site-packages 中而不是安装在我的包目录下?

问题描述 投票:0回答:1

我正在开发一个具有以下结构的Python包:

cfkit/
│
├── README.md
├── setup.py
├── .gitignore
├── LICENSE.txt
├── src/
│   ├── problem.py
│   ├── contest.py
│   ├── __init__.py
│   ├── cmd/
    │   ├── __init__.py
│   │   ├── cli.py
│   ├── client/
    │   ├── __init__.py
│   │   ├── login.py
│   │   ├── submit.py
│   │   ├── fetch.py
│   ├── config/
    │   ├── __init__.py
│   │   ├── config.py
│   │   ├── implementation.py
│   ├── dependencies/
│   │   ├── memory_time_usage.go
│   │   ├── memory_time_usage.exe
│   ├── utils/
    │   ├── __init__.py
│   │   ├── common.py
│   │   ├── constants.py
│   │   ├── variables.py
│   ├── json/
│   │   ├── file1.json
│   │   ├── file2.json
│
├── examples/
│   ├── example1.gif
├── Docs/
│   ├── how_to_use.md

这是我的

setup.py
文件:

from shutil import copy
from pathlib import Path
from platform import uname
from setuptools import setup, find_packages

with open("README.md", 'r', encoding="UTF-8") as file:
    long_description = file.read()

package_data = {
    "src/json": ["json/*.json"],
    "docs": ["docs/*.md"],
    "examples": ["examples/*.gif"],
    '': ["README.md", "LICENSE.txt"],
}

... Some other code

setup(
    name='cfkit',
    version='0.0.1',
    author='Yassine Ghoudi',
    author_email='[email protected]',
    description='A simple Python CLI tool example',
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/ghoudiy/cfkit",
    license="MIT",
    packages=find_packages(where="src"),
    package_data=package_data,
    include_package_data=True,
    python_requires=">=3.8",
    install_requires=[
        'requests',
        'MechanicalSoup',
        'beautifulsoup4',
        'prompt_toolkit'
    ],
    entry_points={
        'console_scripts': [
            'cf = cfkit.cmd.cli:main',
        ],
    },
    classifiers=[
        'Programming Language :: Python :: 3',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
    ],
    keywords="CLI, Tool, Python"
)

我得到了什么

但是,当我运行

pip install .
时,我的项目文件直接保存在site-packages(例如site-packages/utils、site-packages/client)中,而不是放在site-packages/cfkit下。

我想要什么

在生产环境中,我想使用

import cfkit
导入我的包并获取 src/init.py 中写入的内容:

'''
cfkit Documentation
'''
from cfkit.problem import Problem
from cfkit.contest import Contest

以utils/common.py中的代码为例,我想这样导入:

from cfkit.utils.variables import ...

如何修复此问题,以便我的所有软件包文件都正确安装在

site-packages
内的 cfkit 目录下?

错误

这是我遇到的错误

 Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [9 lines of output]
      running egg_info
      creating /tmp/pip-pip-egg-info-2l3_y70l/cfkit.egg-info
      writing /tmp/pip-pip-egg-info-2l3_y70l/cfkit.egg-info/PKG-INFO
      writing dependency_links to /tmp/pip-pip-egg-info-2l3_y70l/cfkit.egg-info/dependency_links.txt
      writing entry points to /tmp/pip-pip-egg-info-2l3_y70l/cfkit.egg-info/entry_points.txt
      writing requirements to /tmp/pip-pip-egg-info-2l3_y70l/cfkit.egg-info/requires.txt
      writing top-level names to /tmp/pip-pip-egg-info-2l3_y70l/cfkit.egg-info/top_level.txt
      writing manifest file '/tmp/pip-pip-egg-info-2l3_y70l/cfkit.egg-info/SOURCES.txt'
      error: package directory 'utils' does not exist
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
python packaging pypi directory-structure
1个回答
0
投票

如果你想安装一个名为

cfkit
的包,你需要在src
定义

cfkit/
│
├── README.md
├── setup.py
├── .gitignore
├── LICENSE.txt
├── src/
│    ├── cfkit/
│          ├── problem.py
│          ├── contest.py
│          ├── __init__.py
│          ├── cmd/
│          │   ├── __init__.py
│          │   ├── cli.py
│          ├── client/
.          .
.          .
.          .
© www.soinside.com 2019 - 2024. All rights reserved.