I use setuptools and a requirements file to satisfy dependencies for my open source module.
Example setup.py:
from setuptools import setup, find_packages
from pip.req import parse_requirements
# parse_requirements() returns generator of
# pip.req.InstallRequirement objects
install_reqs = parse_requirements('requirements.txt',
session=False)
# reqs is a list of requirement
reqs = [str(ir.req) for ir in install_reqs]
setup(
name='python-symphony',
version='0.1.5',
description='python module for symphony chat',
author='Matt Joyce',
author_email='matt#joyce.nyc',
url='https://github.com/symphonyoss/python-symphony',
license='Apache 2.0',
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
],
keywords='symphony chat api python module',
# install dependencies from requirements.txt
install_requires=reqs,
packages=find_packages(),
# bin files / python standalone executable scripts
include_package_data=True,
zip_safe=False,
)
This has worked for some time. However, recently my builds have been breaking. I believe there is a bug in setuptools, possibly related to pygments.
There are two (thus far) identified failure states:
It results in the package being installed as a binary wheel (despite no such package existing in PyPI).
It results in the package installing in site-packages seemingly correctly, but none of the subclasses can be called from the module.
I can get it to work manually by removing the package from site packages and running:
pip install --isolated --no-cache-dir python-symphony
but this results in pygments getting messed up somehow. (I noticed this when trying to debug the module in bpython).
Trying to run this stuff from a venv (wrapped with a Bash script) tends to still fail.
Is anyone aware of a new / recent issue in setuptools that could be responsible for this sort of breakage?
setuptools version: setuptools-33.1.1.dist-info and newer
Ref:
github: https://github.com/symphonyoss/python-symphony
pypi: https://pypi.python.org/pypi/python-symphony/
Related
I want to create a pip package which dependent on some OS specific files:
Let's say there are:
dependency_Windows_x86_64.zip
dependency_Linux_x86_64.zip
dependency_MAC_OS_X.zip
I do not want to include all three archives in a package project, but download them dynamically during the pip install my-package based on user's OS. How can I do that ? Where should I put the code responsible for downloading/unzipping those files ?
My setup.py looks like this:
from setuptools import setup
setup(
name='my-package',
version='0.0.1',
description='Package description',
py_modules=['my_package'],
package_dir={'': 'src'},
classifiers=[
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Operating System :: POSIX :: Linux',
'Operating System :: Microsoft :: Windows',
'Operating System :: MacOS',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.7'
],
python_requires='>=3.7'
)
The platform specific dependencies could be kept in separate Python projects (wrappers around data only packages) and then required from the main project like the following:
# setup.cfg
# ...
[options]
install_requires =
my_package_win_amd64 ; platform_system=="Windows" and platform_machine=="x86_64"
my_package_linux-x86_64 ; platform_system=="Linux" and platform_machine=="x86_64"
This approach doesn't depend on setuptools and could be used with other build systems.
First answer is give up and use setuptools. Look at Platform specific dependencies for a good write up of what to do.
Second answer is to make separate packages such as 'mylib-mac', 'mylib-win', 'mylib-linux'.
Third answer is to use the "console_script" approach. This will break. While it generates .exe files on Windows, it has odd failure modes. Also, some users will not be able to dynamically download files because they work from an internal clone of a repository. Randomly running code from the Internet on production can scare people.
Hope this helps!
A solution could be to publish platform specific Python wheels of your project. The platform specific files could be added to the pre-built distributions via a custom setuptools command (probably a sub-command of build, or maybe install).
This is not a full solution, but something like this might be a good start:
#!/usr/bin/env python3
import distutils.command.build
import setuptools
class build_something(setuptools.Command):
user_options = [
('plat-name=', 'p', "platform name to build for"),
]
def initialize_options(self):
self.plat_name = None
def finalize_options(self):
self.set_undefined_options('bdist_wheel', ('plat_name', 'plat_name'))
def run(self):
print(" *** plat_name: {} ***".format(self.plat_name))
print(" *** download the platform specific bits to 'build' ***")
class build(distutils.command.build.build):
sub_commands = [(
'build_something',
None,
)] + distutils.command.build.build.sub_commands
setuptools.setup(
cmdclass={
'build_something': build_something,
'build': build,
},
# ...
)
And then the Python wheels could be built like this:
$ ./setup.py bdist_wheel -p win_amd64
I'm attempting to publish my module to PyPi but I'm running into troubles. It publishes, and I can install it via Pip, but I cannot seem to figure out the correct import statement to instantiate my class.
This is my setup.py file, the code lives in discord_webhooks.py within the same directory. Here's the published package.
from setuptools import setup, find_packages
long_description = open('README.md').read()
setup(
name='Discord Webhooks',
version='1.0.1',
packages=find_packages(exclude=['tests', 'tests.*']),
url='https://github.com/JamesIves/discord-webhooks',
author='James Ives',
author_email='iam#jamesiv.es',
description='Easy to use package for Python which allows for sending of webhooks to a Discord server.',
long_description=long_description,
license='MIT',
install_requires=[
'requests==2.20.0'
],
classifiers=[
'Programming Language :: Python :: 3'
],
)
I've attempted import DiscordWebhooks, and from discord_webhooks import DiscordWebhooks after doing pip install discord-webhooks but neither seem to work. Any help would be appreciated!
Managed to solve this one on my own. As this is a single file module I need to use py_modules inside of the setup.py file.
Here's the updated file:
from setuptools import setup, find_packages
long_description = open('README.md').read()
setup(
name='Discord Webhooks',
version='1.0.3',
py_modules=['discord_webhooks'],
url='https://github.com/JamesIves/discord-webhooks',
author='James Ives',
author_email='iam#jamesiv.es',
description='Easy to use package for Python which allows for sending of webhooks to a Discord server.',
long_description=long_description,
license='MIT',
install_requires=[
'requests==2.20.0'
],
classifiers=[
'Development Status :: 5 - Production/Stable',
'Environment :: Other Environment',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
],
)
I am developing a PyQt5 app and making it available via pip install now that pip in python3 can install pyqt5 as dependence. I made an entry point to launch my package, and told setup.py that it's a gui_scripts.
What I would like to do now, is after the person typing pip install package, and the installation is finished, display a message to the person telling that you can now type package in the terminal to load the application. What's the correct way of doing that? Or should I not do this?
If you can ensure that
the package is always installed from a source distribution, not a binary wheel, and
the user uses the -v option for pip install,
you can output text in your setup.py script.
The setup.py is almost a regular Python script.
Just use the print() function at the end of your setup.py file.
In this example the file structure is somedir/setup.py, somedir/test/ and test/__init__.py.
Simple solution
from setuptools import setup
print("Started!")
setup(name='testing',
version='0.1',
description='The simplest setup in the world',
classifiers=[
'Development Status :: 3 - Alpha',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.0',
],
keywords='setup',
author='someone',
author_email='someone#example.com',
license='MIT',
packages=['test'],
entry_points={
},
zip_safe=False)
print("Finished!")
Started! running install running bdist_egg running egg_info writing testing.egg-info/PKG-INFO
... ... ...
Processing dependencies for testing==0.1 Finished processing
dependencies for testing==0.1 Finished!
Using setuptools.command.install solution
Also, you can subclass the setuptools.command.install command. Check the difference when you change the order of install.run(self) and os.system("cat testing.egg-info/PKG-INFO") in a clean setup.
from setuptools import setup
from setuptools.command.install import install
import os
class PostInstallCommand(install):
"""Post-installation for installation mode."""
def run(self):
install.run(self)
os.system("cat testing.egg-info/PKG-INFO")
setup(name='testing',
version='0.1',
description='The simplest setup in the world',
classifiers=[
'Development Status :: 3 - Alpha',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.0',
],
keywords='setup',
author='someone',
author_email='someone#example.com',
license='MIT',
packages=['test'],
entry_points={
},
cmdclass={
'install': PostInstallCommand,
},
zip_safe=False)
Another option would be the logging module.
rasterio for example uses an approach like this which you could adapt for your needs:
import logging
import sys
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
log = logging.getLogger()
...
log.info("Your message")
sys.stderr will be printed by pip.
I am writing a Django application and am very new to the framework and python. I am following the tutorial here: https://docs.djangoproject.com/en/1.9/intro/reusable-apps/ and am currently at packaging and installing the application created from this tutorial: https://docs.djangoproject.com/en/1.9/intro/tutorial01/
This is my first Django application and my first time using Python.
Currently my setup.py file looks like this:
import os
from setuptools import find_packages, setup
with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
README = readme.read()
# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
setup(
name= 'django-polls',
version='0.1',
packages=find_packages(),
include_package_date=True,
license='BSD License', #example license
description = 'A simple Django app to conduct Web-based polls.',
long_description = README,
url='https://www.example.com',
author='Your Name',
author_email='yourname#example.com',
classifiers=[
'Environment :: Web Environment',
'Framework :: Django',
'Framework :: Django :: 1.9.6', # replace "X.Y" as appropriate
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License', # example license
'Operating System :: OS Independent',
'Programming Language :: Python',
# Replace these appropriately if you are stuck on Python 2.
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
],
)
my MANIFEST.in file looks like this:
include LICENSE
include README.rst
recursive-include polls/static *
recursive-include polls/templates *
recursive-include docs *
my file directory is as follows:
|polls
|__init.py__
|admin.py
|apps.py
|models.py
|tests.py
|urls.py
|views.py
|__pychache__
|# pycachestuff
|migrations
|# migrations stuff
|static
|polls
|images
|background.gif
|style.css
|templates
|polls
|detail.html
|index.html
|results.html
|docs
|
When I run the setup, the produced .zip works great, all the directories are included. However, when I run
pip install C:\Path\To\dist\django-polls-0.1.zip -t C:\Path\To\Package
It only installs what is immediately under the primary polls directory and migrations. It ignores the subdirectories static and templates. It also ignores docs, but the folder is empty and the tutorial has led me to believe empty directories are ignored.
Does anyone know how I can fix this? A few answers I have seen suggest using package_data or data_files, but I'm very new to python and I can't seem to get the syntax right (or those answers are wrong).
Thank you for your time
I have a Python package that only runs on Python 2. It has the following classifiers in its setup.py:
setup(
# ...
classifiers=[
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2 :: Only',
])
However, if I create a virtualenv with Python 3, pip happily installs this package.
How do I prevent the package being installed? Should my setup.py throw an error based on sys.version_info? Can I stop pip even downloading the package?
In setup.py, add this:
import sys
if sys.version_info[0] != 2:
sys.stderr.write("This package only supports Python 2.\n")
sys.exit(1)
In newer versions of setuptools and pip, if you're using setup.py, here's how to specify a requirement of Python 2 only (specifically Python 2.7):
from setuptools import setup
setup(
name="my_package_name",
python_requires='>=2.7,<3.0',
# ...
)
It would also be good to include classifiers, like this:
setup(
name="my_package_name",
python_requires='>=2.7,<3.0',
classifiers=[
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 2 :: Only",
],
)