... but Python has something like Ruby's bundler No (I don't think it's unified even if it fits) A free directory structure for each product ...
At least I wanted to unify my products Directory structure when creating a Python library Write notes about environment construction. I'll forget ...
I am aware that the version of Python used is 3.3 or later. Prior to that (3.2, 3.1 ... 2.x) is not considered. I don't think it's a problem ...
I made a library called ** fizzbuzz **. It's a library or command line tool. I will write notes based on the structure of this library.
The directory and file structure is as follows.
fizzbuzz
└lib #Library body
└bin #Execution script body
└fizzbuzz #Library name directory
└ __init__.py #Initialization settings when importing the library are here
└test #Test file
requirements.txt # travis-ci and setup.Use with py. pip--Can be made with freeze.
info.py #A file that contains package author information, etc.
version.py #File containing version information
setup.py #File to be the key
I think it's okay because of this.
Starting with Python 3.3, virtualenv
is included.
To test the operation of the library, use the following command
Create a local Python runtime environment.
> pyvenv .venv
I've been using it since virtualenv, so it's under .venv I try to create an environment for pyvenv, but I think it's optional (such as .pvenv).
Since I built a local Python execution environment,
activate
The shellsource
Import with a command.
By doing this, you will be able to use the Python environment under `` `.venv```. (like python command)
> source .venv/bin/activate
Up to a certain version of virtualenv The system library was imported by virtualenv, It cannot be imported as a new version of virtualenv and pyvenv. Running pyvenv builds a pure environment.
Therefore, the environment is created without easy_install
.
Run the following command to set up setuptools
.
Quoted from the PyPI page of setuptools.
> wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py -O - | python
Now run the easy_install command and it's on your PyPI You will be able to install the package. But rather than installing the PyPI package with easy_install
It's easier to use with pip. Let's put pip...
## pip installation
If you have easy_install installed, you can easily install pip.
easy_install pip
Now you are ready.
# Make a list of required libraries
If your library uses another library
In the hash passed to the setuptools.setup function
You have to specify install_requires.
You can easily create a list of required libraries using pip.
pip freeze > requirements.txt
Travis CI also uses requirements.txt
In setup.py, specify install_requires using ``` requirement.txt```.
```py
setup({
install_requires: open('requirements.txt').read().splitlines(),
})
This will install the external libraries required by the library.
Which file should have the library creator information displayed on PyPI
I was at a loss, but I put it as info.py
directly under package root.
info.py
# package information.
INFO = dict(
name = "PyFizzBuzz",
description = "FizzBuzz cli tool",
author = "Keiji Matsuzaki",
author_email = "[email protected]",
license = "MIT License",
url = "https://github.com/futoase/fizzbuzz",
classifiers = [
"Programming Language :: Python :: 3.2",
"Programming Language :: Python :: 3.3",
"License :: OSI Approved :: MIT License"
]
)
classifiers have their own library from List here Select and set the genre that seems to match.
version.Specify VERSION in py.
#### **`version.py`**
```py
VERSION = "0.0.3"
** 20131017 postscript **
package_info in dir.py, version.py, requirements.Because you didn't specify a txt file
I have to write a MANIFEST.in file.
I didn't check it when I wrote this article, so I couldn't find any issues that weren't included.
So add it.
#### **`MANIFEST.in`**
```py
include info.py
include version.py
include requirements.txt
Test the package using the pip command
> pip install .
By specifying yourself in your own directory and doing a pip install,
pyvenv
You can use your own library in your environment.
> pip list
konira (0.3.2)
pip (1.4.1)
PyFizzBuzz (0.0.3)
setuptools (1.1.5)
If you find a bug in your library after pip install Execute the following command under the environment of pyvenv.
> pip uninstall PyFizzBuzz
It's like specifying gemspec
in the Gemfile ... I think you should execute the command with feelings ...
This time, fizzbuzz is executed as a command, so
.venv/bin/The command is located in fizzbuzz.
(.venv) > fizzbuzz 10 | head -3
1
2
Fizz
I was able to check the operation.
# Register with PyPI
If you have not registered to PyPI, execute the following command to register.
#### **`~/.If you have not generated a piprc file, you will be prompted to register your user name and password.(Should be)...`**
> python setup.py register
Upload the package to PyPI.
> python setup.py sdist upload
OK if a library page is created on PyPI
It may be different for C extension libraries. (I don't consider uploading egg files ...)
The number of downloads per day, a list of files of versions uploaded in the past, etc. You can see it from the PyPI page. (Login required)
It's pretty fun.
Since setuptools is integrated with distribute, only setuptools is imported, but Before the integration, the packages to be imported were separated by setup.py in the following form.
setup.py
try:
import setuptools
except ImportError:
from distribute_setup import use_setuptools
use_setuptools()
from setuptools import setup, find_packages
Distribute_setup.py was dropped from here (I can't access it right now), and it was included in the package.
I'm glad it was unified with setuptools. (This article may be possible because it doesn't consider backward compatibility ...)
Recommended Posts