点安装.仅创建dist-info而不创建包 [英] pip install . creates only the dist-info not the package

查看:261
本文介绍了点安装.仅创建dist-info而不创建包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作要在本地使用pip install .安装的python软件包.软件包名称在pip freeze中列出,但import <package>导致错误No module named <package>.同样,site-packages文件夹仅包含一个dist-info文件夹. find_packages()是可以找到程序包.我想念什么?

I am trying to make a python package which I want to install using pip install . locally. The package name is listed in pip freeze but import <package> results in an error No module named <package>. Also the site-packages folder does only contain a dist-info folder. find_packages() is able to find packages. What am I missing?

import io
import os
import sys
from shutil import rmtree

from setuptools import find_packages, setup, Command

# Package meta-data.
NAME = '<package>'
DESCRIPTION = 'description'
URL = ''
EMAIL = 'email'
AUTHOR = 'name'

# What packages are required for this module to be executed?
REQUIRED = [
    # 'requests', 'maya', 'records',
]

# The rest you shouldn't have to touch too much :)
# ------------------------------------------------
# Except, perhaps the License and Trove Classifiers!
# If you do change the License, remember to change the Trove Classifier for that!

here = os.path.abspath(os.path.dirname(__file__))



# Where the magic happens:
setup(
    name=NAME,
    #version=about['__version__'],
    description=DESCRIPTION,
    # long_description=long_description,
    author=AUTHOR,
    author_email=EMAIL,
    url=URL,
    packages=find_packages(),
    # If your package is a single module, use this instead of 'packages':
    # py_modules=['mypackage'],

    # entry_points={
    #     'console_scripts': ['mycli=mymodule:cli'],
    # },
    install_requires=REQUIRED,
    include_package_data=True,
    license='MIT',
    classifiers=[
        # Trove classifiers
        # Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
        'Programming Language :: Python :: 3.6',
        'Programming Language :: Python :: Implementation :: CPython',
        'Programming Language :: Python :: Implementation :: PyPy'
    ],

)

推荐答案

由于该问题已非常流行,因此,在安装后丢失文件时,可以通过以下诊断步骤进行操作.假设有一个具有以下结构的示例项目:

Since the question has become quite popular, here are the diagnosis steps to go through when you're missing files after installation. Imagine having an example project with the following structure:

root
├── spam
│   ├── __init__.py
│   ├── data.txt
│   ├── eggs.py
│   └── fizz
│       ├── __init__.py
│       └── buzz.py
├── bacon.py
└── setup.py

现在我运行pip install .,检查是否已安装软件包:

Now I run pip install ., check that the package is installed:

$ pip list
Package    Version
---------- -------
mypkg      0.1    
pip        19.0.1 
setuptools 40.6.3 
wheel      0.32.3 

,但在属于已安装软件包的文件列表中,既看不到spamspam/eggs.pybacon.py也不是spam/fizz/buzz.py:

but see neither spam, nor spam/eggs.py nor bacon.py nor spam/fizz/buzz.py in the list of files belonging to the installed package:

$ pip show -f mypkg
Name: mypkg
Version: 0.1
...
Files:
  mypkg-0.1.dist-info/DESCRIPTION.rst
  mypkg-0.1.dist-info/INSTALLER
  mypkg-0.1.dist-info/METADATA
  mypkg-0.1.dist-info/RECORD
  mypkg-0.1.dist-info/WHEEL
  mypkg-0.1.dist-info/metadata.json
  mypkg-0.1.dist-info/top_level.txt

那现在该怎么办?

除非被告知不要这样做,否则pip将始终尝试构建wheel文件并从中安装软件包.如果以详细模式重新安装,我们可以检查日志以了解轮的构建过程.第一步是卸载软件包:

Unless told not to do so, pip will always try to build a wheel file and install your package from it. We can inspect the log for the wheel build process if reinstalling in the verbose mode. First step is to uninstall the package:

$ pip uninstall -y mypkg
...

然后再次安装它,但是现在带有附加参数:

then install it again, but now with an additional argument:

$ pip install . -vvv
...

现在,如果我检查日志:

Now if I inspect the log:

$ pip install . -vvv | grep 'adding'
  adding 'mypkg-0.1.dist-info/METADATA'
  adding 'mypkg-0.1.dist-info/WHEEL'
  adding 'mypkg-0.1.dist-info/top_level.txt'
  adding 'mypkg-0.1.dist-info/RECORD'

我注意到在任何地方都没有提到spam目录或bacon.py中的文件.这意味着它们根本没有包含在wheel文件中,因此没有被pip安装.最常见的错误源是:

I notice that no files from the spam directory or bacon.py are mentioned anywhere. This means they were simply not included in the wheel file and hence not installed by pip. The most common error sources are:

确认已将packages参数传递给设置函数.检查您是否提到了应该安装的所有软件包的所有.如果仅提及父包,则不会自动收集子包!例如,在设置脚本中

Verify you have passed the packages argument to the setup function. Check that you have mentioned all of the packages that should be installed. Subpackages will not be collected automatically if only the parent package is mentioned! For example, in the setup script

from setuptools import setup

setup(
    name='mypkg',
    version='0.1',
    packages=['spam']
)

将安装

spam,但不会安装spam.fizz,因为它本身是一个软件包,必须明确提及.修复它:

spam will be installed, but not spam.fizz because it is a package itself and must be mentioned explicitly. Fixing it:

from setuptools import setup

setup(
    name='mypkg',
    version='0.1',
    packages=['spam', 'spam.fizz']
)

如果您有很多软件包,请使用setuptools.find_packages来自动执行该过程:

If you have lots of packages, use setuptools.find_packages to automate the process:

from setuptools import find_packages, setup

setup(
    name='mypkg',
    version='0.1',
    packages=find_packages()  # will return a list ['spam', 'spam.fizz']
)

如果您缺少模块:

在上面的示例中,安装后我将丢失bacon.py,因为它不属于任何软件包.我必须在单独的参数py_modules中提供其模块名称:

In the above examples, I will be missing bacon.py after installation since it doesn't belong to any package. I have to provide its module name in the separate argument py_modules:

from setuptools import find_packages, setup

setup(
    name='mypkg',
    version='0.1',
    packages=find_packages(),
    py_modules=['bacon']
)

缺少数据文件:检查package_data参数

我现在已放置所有源代码文件,但仍未安装data.txt文件.位于软件包目录下的数据文件应通过package_data参数添加.修复以上安装脚本:

Missing data files: check the package_data argument

I have all the source code files in place now, but the data.txt file is still not installed. Data files located under package directories should be added via the package_data argument. Fixing the above setup script:

from setuptools import find_packages, setup

setup(
    name='mypkg',
    version='0.1',
    packages=find_packages(),
    package_data={'spam': ['data.txt']},
    py_modules=['bacon']
)

不要试图使用data_files参数.将数据文件放在包下,并配置package_data.

Don't be tempted to use the data_files argument. Place the data files under a package and configure package_data instead.

如果我现在重新安装软件包,我会注意到所有文件都已添加到转盘中:

If I now reinstall the package, I will notice all of the files are added to the wheel:

$ pip install . -vvv | grep 'adding'
  adding 'bacon.py'
  adding 'spam/__init__.py'
  adding 'spam/data.txt'
  adding 'spam/eggs.py'
  adding 'spam/fizz/__init__.py'
  adding 'spam/fizz/buzz.py'
  adding 'mypkg-0.1.dist-info/METADATA'
  adding 'mypkg-0.1.dist-info/WHEEL'
  adding 'mypkg-0.1.dist-info/top_level.txt'
  adding 'mypkg-0.1.dist-info/RECORD'

它们也将在属于mypkg的文件列表中可见:

They will also be visible in the list of files belonging to mypkg:

$ pip show -f mypkg
Name: mypkg
Version: 0.1
...
Files:
  __pycache__/bacon.cpython-36.pyc
  bacon.py
  mypkg-0.1.dist-info/INSTALLER
  mypkg-0.1.dist-info/METADATA
  mypkg-0.1.dist-info/RECORD
  mypkg-0.1.dist-info/WHEEL
  mypkg-0.1.dist-info/top_level.txt
  spam/__init__.py
  spam/__pycache__/__init__.cpython-36.pyc
  spam/__pycache__/eggs.cpython-36.pyc
  spam/data.txt
  spam/eggs.py
  spam/fizz/__init__.py
  spam/fizz/__pycache__/__init__.cpython-36.pyc
  spam/fizz/__pycache__/buzz.cpython-36.pyc
  spam/fizz/buzz.py

这篇关于点安装.仅创建dist-info而不创建包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆