对于许多python库,与import
一起使用的参数与使用pip安装库的参数相同。
例如。
pip install numpy
pip install scipy
pip install pandas
对应
import numpy
import scipy
import pandas
但这种模式似乎并不适用于所有的库。例如(发现here):
pip install Pillow
才能让这一切成功
import PIL
根据第一个示例中的模式,我希望pip install PIL
安装PIL
,但我们使用pip install Pillow
。这是为什么,这是如何运作的?
发布于 2020-07-10 12:34:05
基本上,您导入的通常是模块名称。例如,您的包可以在以下层次结构中开发:
MyLib
- __init__.py
- my_script1.py
- my_script2.py
但是,当您将库作为pip
中的“包”可用时,通常需要准备您的setup.py
文件,当人们使用pip install
安装您的包时,该文件将自动运行。
setup.py
可以是这样的:
from distutils.core import setup
setup(
name = 'YOURPACKAGENAME', # How you named your package folder (MyLib)
packages = ['YOURPACKAGENAME'], # Chose the same as "name"
version = '0.1', # Start with a small number and increase it with every change you make
license='MIT', # Chose a license from here: https://help.github.com/articles/licensing-a-repository
description = 'TYPE YOUR DESCRIPTION HERE', # Give a short description about your library
author = 'YOUR NAME', # Type in your name
author_email = 'your.email@domain.com', # Type in your E-Mail
url = 'https://github.com/user/reponame', # Provide either the link to your github or to your website
download_url = 'https://github.com/user/reponame/archive/v_01.tar.gz', # I explain this later on
keywords = ['SOME', 'MEANINGFULL', 'KEYWORDS'], # Keywords that define your package best
install_requires=[ # I get to this in a second
'validators',
'beautifulsoup4',
],
classifiers=[
'Development Status :: 3 - Alpha', # Chose either "3 - Alpha", "4 - Beta" or "5 - Production/Stable" as the current state of your package
'Intended Audience :: Developers', # Define that your audience are developers
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: MIT License', # Again, pick a license
'Programming Language :: Python :: 3', #Specify which pyhton versions that you want to support
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
)
因此,在上面的示例中,通过pip
安装包的人应该运行pip install YOURPACKAGENAME
。之后,他们需要在代码中运行import MyLib
。
TD;DL:
您导入的是模块名,但是通过pip
安装的是包的名称,它们可以是不同的。但通常,我会说,我喜欢人们使用相同的名字,两者,以避免任何混淆。
参考文献:https://medium.com/@joel.barmettler/how-to-upload-your-python-package-to-pypi-65edc5fe9c56
https://stackoverflow.com/questions/62834202
复制相似问题