I want to build a custom formatter for class and function names.
According to this doc it says that the naming convention falls under the N8** warning code.
After following this tutorial with the help of a sublink this is the resultant code
setup.py
from __future__ import with_statement
import setuptools
requires = [
"flake8 > 3.0.0",
]
setuptools.setup(
name="flake8_example",
license="MIT",
version="0.1.0",
description="our extension to flake8",
author="Me",
author_email="example#example.com",
url="https://gitlab.com/me/flake8_example",
packages=[
"flake8_example",
],
install_requires=requires,
entry_points={
'flake8.extension': [
'N8 = flake8_example:Example',
],
},
classifiers=[
"Framework :: Flake8",
"Environment :: Console",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 3",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Software Development :: Quality Assurance",
],
)
flake8_example.py
from flake8.formatting import base
class Example(base.BaseFormatter):
"""Flake8's example formatter."""
def format(self, error):
return 'Example formatter: {0!r}'.format(error)
I setup it up by running pip install --editable .
Then to test it out I ran flake8 --format=example main.py
It throws this error:
flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "N8" due to 'module' object has no attribute 'Example'.
So if you're trying to write a formatter you need to read the documentation a bit more closely. In the registering section of the documentation it says:
Flake8 presently looks at three groups:
flake8.extension
flake8.listen
flake8.report
If your plugin is one that adds checks to Flake8, you will use
flake8.extension. If your plugin automatically fixes errors in code,
you will use flake8.listen. Finally, if your plugin performs extra
report handling (formatting, filtering, etc.) it will use
flake8.report.
(Emphasis mine.)
That means your setup.py should look like this:
entry_points = {
'flake8.report': [
'example = flake8_example:Example',
],
}
If your setup.py properly installs your package then running
flake8 --format=example ...
Should work just fine. The exception you're seeing, however, is due to the module either not having a class named Example. You should investigate whether packages will pick up a single file module or if you need to restructure your plugin so that it looks like:
flake8_example/
__init__.py
...
As that may be why your setup.py is not working appropriately.
Related
I'm trying to push a project package to pypi (it's my first time doing so) from gitlab, and I'm following their turorial [here][1]. However, when I am trying python3 -m twine upload --repository gitlab dist/* the console returns me HTTPError: 422 Unprocessable Entity from https://gitlab.com/MyProfile/mylib
I don't know what is wrong ?
my .pypirc file looks like this
[distutils]
index-servers =
gitlab
[gitlab]
repository = https://gitlab.com/FiiireFlyyy/firelib
username = my_token
password = my_token_password
and here is my setup.py
# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path
# The directory containing this file
HERE = path.abspath(path.dirname(__file__))
# Get the long description from the README file
with open(path.join(HERE, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
# This call to setup() does all the work
setup(
name="firelib",
version="0.1.0",
description="Demo library",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://gitlab.com/FiiireFlyyy/firelib",
author="Willy Lutz",
author_email="myemail0willy#gmail.com",
license="no licence",
classifiers=[
"Intended Audience :: Developers",
"License :: OSI Approved :: no licence",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Operating System :: OS Independent"
],
packages=["firelib"],
include_package_data=True,
install_requires=["os", "shutil"]
)
My file tree is :
firelib (pycharm project folder)
|-----dist (directory)
|-----firelib (package)
|-----firelib.egg-info (directory)
|-----venv
|-----LICENCE
|-----README
|-----setup.py
|-----requirements.txt
I'm really bad with config stuff. Can someone help me ?
Edit : i put the real neames of packages insteand of 'myProfile' and 'mylib'
[1]: https://docs.gitlab.com/ee/user/packages/pypi_repository/
I have a package which I have written and installed onto a virtual environment in editable mode. I can only import from this package only when the modules and items within those modules are imported using the 'from' syntax.
In a python file outside the package, I can import specific modules from the package using from package import module and import specific functions/objects from these modules via from package.module import x in external scripts/python interpreter.
However, when I try to import the whole module, I find that the package has no accessible modules; i.e. if I were to write:
import package
x = package.module.x
Then I would receive the error:AttributeError: module 'package' has no attribute 'module'.
Intriguingly , if I use a from import and then attempt the same command again, the error does not occur and the attribute, object or function 'x' imports properly.
I believe that the problem should have something to do with how the package is found in the setup.py, but I don't know enough about python packaging to understand what is going on
I have this module installed in editable mode through pip on my anaconda virtual environment, and it uses the standard cookiecutter python format. There are no sub-packages, and the init.py contains only basic bibliographic information ('_name_ ' and '_email_' and so on).
here is my setup:
"""The setup script."""
from setuptools import setup, find_packages
with open('README.rst') as readme_file:
readme = readme_file.read()
with open('HISTORY.rst') as history_file:
history = history_file.read()
requirements = ['Click>=7.0', ]
test_requirements = ['pytest>=3', ]
setup(
author="Alexander Pasha",
author_email={email},
python_requires='>=3.9',
classifiers=[
'Development Status :: 2 - Pre-Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Natural Language :: English',
'Programming Language :: Python :: 3.9',
],
description={description},
install_requires=requirements,
license="MIT license",
long_description=readme + '\n\n' + history,
include_package_data=True,
keywords={name},
name={name},
packages=find_packages(include=['package', 'package.*']),
test_suite='tests',
tests_require=test_requirements,
url={GitHub},
version='0.1.0',
zip_safe=False,
)
for reference, I'm using python 3.9.12
I'd like to ask a question about how to configure setup.py with Cython, setuptools extensions etc. I'm trying cythonize a submodule with Cython.Distutils's build_ext. But the issue isn't Cython.Distutils's Extentsion module, (if it exists) because it isn't loaded. Only build_ext. So I create a setuptools.extension Extension in a list, and then cythonize the list of Extension objects. There's only one Extension in the list, an its as follows.
Extension("distance", ["kmerdb/distance.pyx"], include_dirs=[np.get_include()])
I've tried different methods of installation, from python setup.py install to pip install -e . to generating the wheel file and installing the wheel. I couldn't find anything that worked...
Okay, so I know very little about the process and that's probably why I am getting hung up but I've searched the whole site, no luck. So here goes.
I'm running the following shell script to install my package locally, and it works fine. The problem is the installation doesn't move the .so and the .py file into the kmerdb module I'm trying to export. Any suggestions? Thanks
>python setup.py sdist bdist_wheel
>/bin/auditwheel repair --plat manylinux2014_x86_64 dist/kmerdb-*linux_x86_64.whl
>mv wheelhouse/* dist
>rm dist/*linux_x86_64.whl
>pip install dist/kmerdb-*-manylinux2014_x86_64.whl
>ls ~/.pyenv/versions/kdb/lib/python3.10/site-packages/kmerdb-0.6.5-py3.10-linux-x86_64.egg/
distance.cpython-310-x86_64-linux-gnu.so distance.py kmerdb ...
Again, the files distance.cpython-310-x86_64-linux-gno.so is not moved into the kmerdb module, my Python packages, which I'm trying to install locally and configure for .whl upload to PyPI.
Python 3.10.1 (main, Jan 1 2022, 21:28:19) [GCC 11.1.0] on linux
Cython==0.29.26
setup.py
Extension("distance", ["kmerdb/distance.pyx"], include_dirs=[np.get_include()], define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")],),
setup(
name=NAME,
version=VERSION,
description=DESCRIPTION,
long_description=long_description,
long_description_content_type='text/markdown',
author=AUTHOR,
author_email=EMAIL,
python_requires=REQUIRES_PYTHON,
url=URL,
download_url=CURRENT_RELEASE,
keywords = ["k-mer", "kmer", "k-merdb", "kmerdb", "kdb"],
classifiers=[
"Development Status :: 1 - Planning",
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Topic :: Scientific/Engineering",
"Topic :: Scientific/Engineering :: Bio-Informatics",
"Topic :: Software Development :: Libraries :: Python Modules",
],
packages=find_packages(exclude=["tests", "*.tests", "*.tests.*", "tests.*"]),
package_dir={'kmerdb': 'kmerdb'},
package_data={'kmerdb': ['CITATION']},
# If your package is a single module, use this instead of 'packages':
#py_modules=['kmerdb'],
#scripts=['bin/kmerdb', 'bin/kmerdb_report.R'],
entry_points={
'console_scripts': ['kmerdb=kmerdb:cli'],
},
install_requires=REQUIRED,#['Cython==0.29.21', 'numpy==1.18.1'],
extras_require=EXTRAS,
include_package_data=True,
license='GPLv3+',
test_suite='test',
# tests_require=['mamba', 'expect'],
ext_modules=cythonize(extensions),
library_dirs=["."],
zip_safe=False,
)
I'm going to self answer here. The issue stemmed from an improperly specified Extension.
Extension("kmerdb.distance", ["kmerdb/distance.pyx"], include_dirs=[np.get_include()])
All I had to do was include the module name for the fully specified submodule hierarchy. Fixed it!
How to process the correct build of my application to PIP?
I have done everything like a need in the documentation and it works, but after I have updates and my scripts changed from one to few (started from "main.py" script which imported others).
And my build process is broken now. How I able to fix this?
setup.py
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name='tests',
version='0.0.2',
scripts=['tests'] ,
author="Test",
author_email="test#test.com",
description="TEST",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://test.com",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: Unix"
],
)
Where "platops" is a directory with scripts.
Error
error: [Errno 21] Is a directory: 'tests'
How to correct build this?
It seems like you can't add a directory in scripts=[]. You can read up on it here. You will probably need to specify the relative path to each one.
From the docs:
Scripts are **files** containing Python source code, intended to be
started from the command line.
Edit: You could also try using globbing:
scripts=['scripts/*']
I have published a module to pypi called 'surrealism' that generates surreal sentences and error messages. It contains a SQLite3 database containing all of the words and sentences needed for my module.
All of the following install methods work fine:
python setup.py install
pip install surrealism
easy_install surrealism
and the module works fine.
However, when installing into a virtualenv, things go wrong. surrealism.py get installed into C:\Users\me\virtualenvs\surrealism\Lib\site-packages, but surrealism.sqlite doesn't get installed?
If I run python and try and import the module, my module creates a new sqlite3 database called surrealism.sqlite at C:\Users\me\virtualenvs\surrealism
The contents of my setup.py follows:
#!/usr/bin/env python
from setuptools import setup
long_desc = open('readme.rst').read()
setup(name = 'surrealism',
version = '0.5.2',
py_modules = ['surrealism'],
author = 'Morrolan',
author_email = 'morrolan#icloud.com',
url = 'https://github.com/Morrolan/surrealism',
license = 'GNU General Public License (GPL)',
description = 'Surreal sentence and error message generator.',
long_description = long_desc,
platforms = ['Windows','Unix','OS X'],
download_url = "https://pypi.python.org/pypi/surrealism/",
keywords = ["surreal", "surrealism", "error message"],
classifiers = [
"Programming Language :: Python",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.3",
"Development Status :: 4 - Beta",
"Environment :: Other Environment",
"Intended Audience :: Developers",
"Intended Audience :: Education",
"License :: OSI Approved :: GNU General Public License (GPL)",
"Operating System :: OS Independent",
"Topic :: Education",
"Topic :: Software Development :: Libraries :: Python Modules",
],
install_requires=['setuptools'],
)
In surrealism.py, I reference/connect to the SQLite3 database in a fairly rudimentary way:
CONN = sqlite3.connect('surrealism.sqlite')
But so far it hasn't caused me any problems.
is there a more explicit way to reference surrealism.sqlite, or do I have to specify something in setup.py to force the installation?
Kind Regards,
Morrolan
the crucial problem is exactly the way you are connecting to your sqlite database; That will refer to a file in the current directory;, anywhere the program that invokes it is trying to run. What you want to say is
... sqlite3.connect(where_this_python_lib_is_installed + '...sqlite')
So that it doesn't matter where it's installed. There is a fairly standard way to do that, using the pkg_resources library. Since we're trying to discover a sqlite database, that means we need a real file on disk, not a string or file-like object; so the right method to use here pkg_resources.resource_filename, we just need to change the connect call to:
from pkg_resources import resource_filename
CONN = sqlite3.connect(resource_filename(__name__, 'surrealism.sqlite'))
But wait... That only works if the package data is in a package, but you currently have a module. Not a big problem, though; we'll rename surrealism.py to surrealism/__init__.py, surrealism.sqlite to surrealism/surrealism.sqlite, and make the appropriate changes in MANIFEST.in. We'll also need to tell setuptools about this. Change py_modules=["surrealism"], in your setup.py to packages=["surrealism"].
Almost there, The last thing we need to do is get setuptools to actually install that file from source. The first is pretty obvious, we need to tell it which files to copy; Add
package_data={'surrealism': ['surrealism.sqlite']},
To your setup.py, the second change is more subtle. In most cases, setuptools tries to install packages as zip files. This is usually a good thing; but in our case, we need to pass the filename of a real file to sqlite.connect, so we have to tell it not to try to zip the package. For that, just add
zip_safe=False,
To your setup.py.