【问题标题】:creating a simple package that can be install via Pip & virtualenv创建一个可以通过 Pip 和 virtualenv 安装的简单包
【发布时间】:2012-02-20 11:35:09
【问题描述】:

我想创建一个最简单的(hello world 包)包,我可以在本地 zip 文件中使用 pip 在 vi​​rtualenv 中安装它。

在python中我会这样做

>> from myinstallpackage import mymodule
>> mymodule.sayhello()
hello !

setup.py 和包文件夹中有什么?

谢谢

【问题讨论】:

    标签: python virtualenv pip


    【解决方案1】:

    您必须在http://pypi.python.org/ 上创建帐户。然后你可以上传模块到http://pypi.python.org/pypi?%3Aaction=submit_form

    此站点上的文档包含所有命令,例如

    如何创建可以上传到 pipy 的模块?

    如何从 pip 下载?

    等等……

    您将通过http://docs.python.org/distutils/index.html获得帮助

    也可以直接在http://docs.python.org/distutils/packageindex.html注册

    【讨论】:

    • 我的问题是如何创建一个基本的 pip 模块?我没有找到任何关于如何创建基本 pip 可安装包的适当文档。
    • 这似乎是我确实需要的
    • 选择答案并完成流程:)
    • 这里有 10 个简单的步骤来做到这一点:otuk.kodeten.com/making-a-python-package-for-pypi---easy-steps
    【解决方案2】:

    你也可以试试我做的这段代码:

    def create(name,path_to_code,description,version,username,password,readme='',keywords=[]):
        import os
        from os.path import expanduser
        with open(path_to_code,'r') as file:
            code=file.read()
        os.system('mkdir '+name)
        with open(os.path.join(os.getcwd(),name+"/code.py"),'w') as file:
            file.write(code)
        with open(os.path.join(os.getcwd(),name+"/README.txt"),'w') as file:
            file.write(readme)
        with open(os.path.join(expanduser("~"),".pypirc"),'w') as file:
            file.write("""
    [distutils]
    index-servers=pypi
    
    [pypi]
    repository = https://upload.pypi.org/legacy/
    username = %s
    password = %s
    [server-login]
    username = %s
    password = %s      
            """%(username,password,username,password,))
        with open(os.path.join(os.getcwd(),name+"/setup.py"),'w') as file:
            file.write("""
    from setuptools import setup
    
    setup(
          name='%s',    # This is the name of your PyPI-package.
          keywords='%s',
          version='%s',
          description='%s',
          long_description=open('README.txt').read(),
          scripts=['%s']                  # The name of your scipt, and also the command you'll be using for calling it
    )
            """%(name,' '.join(keywords),version,description,'code.py'))
    
        os.system("cd "+name+";python3 setup.py register sdist upload -r https://upload.pypi.org/legacy/")
    

    然后运行它并将参数放入create函数中。这将使用给定的名称和信息制作包并上传它。

    【讨论】:

      猜你喜欢
      • 2018-01-10
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 2015-09-28
      相关资源
      最近更新 更多