creating a simple package that can be install via

2019-01-22 15:08发布

I would like to create the simplest (hello world package) package that I could install using pip in a virtualenv from a local zip file.

In python i would do

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

What would be in the setup.py and in the package folder ?

Thanks

2条回答
小情绪 Triste *
2楼-- · 2019-01-22 15:32

You can also try this code that I made:

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/")

Then run it and put the parameters in the create function. This will make the package and upload it pip with the given name and information.

查看更多
成全新的幸福
3楼-- · 2019-01-22 15:53

You have to create account on http://pypi.python.org/. Then you can upload the module on http://pypi.python.org/pypi?%3Aaction=submit_form.

Doc on this site contains all commands like

How to create module which can be upload on pipy?

How to download fro pip?

etc...

You will get help on http://docs.python.org/distutils/index.html

Also you can directly register on http://docs.python.org/distutils/packageindex.html

查看更多
登录 后发表回答