创建一个可以通过Pip和virtualen安装的简单包

2024-05-29 10:32:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我会用python

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

setup.py和package文件夹中的内容是什么?

谢谢


Tags: pip文件frompyimportpackagehelloworld
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函数中。这将使包和上传它与给定的名称和信息pip。

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

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

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

如何下载fro pip?

等等。。。

您将获得有关http://docs.python.org/distutils/index.html的帮助

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

相关问题 更多 >

    热门问题