首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么与pip install一起使用的参数有时与import使用的参数不同?

为什么与pip install一起使用的参数有时与import使用的参数不同?
EN

Stack Overflow用户
提问于 2020-07-10 12:20:24
回答 1查看 596关注 0票数 1

对于许多python库,与import一起使用的参数与使用pip安装库的参数相同。

例如。

代码语言:javascript
运行
复制
pip install numpy
pip install scipy
pip install pandas

对应

代码语言:javascript
运行
复制
import numpy
import scipy
import pandas

但这种模式似乎并不适用于所有的库。例如(发现here):

代码语言:javascript
运行
复制
pip install Pillow

才能让这一切成功

代码语言:javascript
运行
复制
import PIL

根据第一个示例中的模式,我希望pip install PIL安装PIL,但我们使用pip install Pillow。这是为什么,这是如何运作的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-10 12:34:05

基本上,您导入的通常是模块名称。例如,您的包可以在以下层次结构中开发:

代码语言:javascript
运行
复制
MyLib
- __init__.py
- my_script1.py
- my_script2.py

但是,当您将库作为pip中的“包”可用时,通常需要准备您的setup.py文件,当人们使用pip install安装您的包时,该文件将自动运行。

setup.py可以是这样的:

代码语言:javascript
运行
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62834202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档