Cython compiling to two locations - python

I have a setup.py file and I want to take .pyx files from deeprai/engine/cython/ and compile the .so files and dump those files in the same deeprai/engine/cython/ directery. Right now with my current code, it compiles to deeprai/engine/cython/which is wanted but it also compiles to the root folder
import sys
from setuptools import setup
from Cython.Build import cythonize
from setuptools import find_packages
from setuptools.extension import Extension
from setuptools.command.build_ext import build_ext as _build_ext
with open("README.md", "r") as f:
long_description = f.read()
extensions = cythonize("deeprai/engine/cython/*.pyx")
class build_ext(_build_ext):
def build_extension(self, ext):
ext.extra_compile_args.append('-g')
if sys.platform == 'win32':
print("s")
ext.extra_compile_args.append('/Zi')
ext.filename = ext.name + '.pyd'
else:
ext.filename = ext.name + '.so'
_build_ext.build_extension(self, ext)
setup(
name='deeprai',
version='0.0.3',
description='A easy to use and beginner friendly neural network tool box that anyone can pick up and explorer machine learning! ',
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/Deepr-ai/Deepr-ai",
packages=find_packages(),
ext_modules=extensions,
options={'build_ext': {'build_lib': 'deeprai/engine/cython/'}},
package_data={'deeprai.engine.cython': ['engine/cython/*.pyd', 'engine/cython/*.so']},
cmdclass={'build_ext': build_ext},
install_requires=[
'cython',
'numpy',
'pyndb',
'alive-progress',
'colorama'
],
classifiers=["Programming Language :: Python :: 3",
"Programming Language :: Cython",
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",]
)
I've tried using many different things like modifying the cythonize arguments but it's still the same result every time.

Related

Add other files in Python distribution

I am working on a project which I have to share as a standalone SDK with my team. For some reason files other than *.py are not being added to my distribution file
My project structure is as given below
My Setup.py is like this
import setuptools
from glob import glob
from os.path import basename
from os.path import splitext
with open("README.md", "r") as fh:
long_description = fh.read()
with open('requirements.txt') as f:
install_requires = f.read().splitlines()[2:]
with open('requirements-dev.txt') as f:
tests_require = f.read().splitlines()[2:]
extras = {
'test': tests_require,
}
setuptools.setup(
name='eu-taxonomy-sdk',
version='0.0.2',
author='Bhushan Fegade',
author_email='bhushan.fegade#morningtar.com',
description='a calculation library for eu-taxonomy',
long_description=long_description,
long_description_content_type='text/markdown',
url='https://msstash.morningstar.com/scm/dataapi/eu-taxonomy-sdk.git',
packages=setuptools.find_packages(where='src'),
package_dir={'': 'src'},
package_data={'eu-taxonomy-sdk': [ 'config/*', 'category/config/*' ]},
py_modules=[splitext(basename(path))[0] for path in glob('src/*.py')],
license='Apache Software License',
classifiers=[
'Programming Language :: Python :: 3',
'License :: OSI Approved :: Apache Software License',
'Operating System :: OS Independent',
],
install_requires=install_requires,
python_requires='~=3.8',
tests_require=tests_require,
extras_require=extras,
test_suite='tests'
)
I am using the following commands to generate a distributable file
python setup.py build
python setup.py sdist
I have tried making some changes in the setup.py file but the distributable file inside the config folder is always empty with only the init.py file and no other file is present.
I want to know if there is a way to keep those other files(CSV, parq) in the final distributable file.
Add a MANIFEST.in file that describes the files you need included.
E.g.
recursive-include src *.csv *.parq

python package not packaging files under a subdirectory

Here is my setup.py
from setuptools import setup, find_packages
import versioneer
REQUIRES = [
"requests",
"subprocess32",
"systemd"
]
CLASSIFIERS = [
...
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6"
]
setup(
author="IBM",
cmdclass=versioneer.get_cmdclass(),
description="A collections of utilities",
entry_points={},
install_requires=REQUIRES,
keywords=["management", "segment_sqls"],
license="XXX",
long_description="""Utilities for instance management and house keeping""",
packages=find_packages(),
tests_require=["pytest"],
url="https://github.ibm.com/myrepo/management",
name="management",
version=versioneer.get_version(),
classifiers=CLASSIFIERS
)
I am trying to run python3 setup.py sdist upload -r local to package and upload to my artifactory.The package's tar.gz is uploaded to artifactory with all files except the ones under segment_sqls. Here is my directory structure. init.py under segment_sqls is packaged but not the actual sql files. Is there any additional param that needs to be specified in setup.py to include all files?

How to package my python program so the user can install it using setup.py

I have a single python file right now and I am asked to convert it into a python module where the user can install it using python setup.py install. I am not sure how to do that. I have followed some instructions online and created the setup.py file and the init.py file. The setup.py file looks like this:
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="",
version="0.0.1",
author="",
author_email="",
description="",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/sampleproject",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
I am not sure if this setup.py file is correct.Also I don't know what I am supposed to do next. Can anyone help me and tell me what am I supposed to do? Is there tutorial that teaches this? I can't really find anything related. Also is my setup.py correct? Thanks!
There are several ways to do packaging. packaging Python Projects on python.org and setuptools docs are a good start.
Unfortunately, examples tend to focus on package distributions, not single modules. Instead of packages, use the py_modules keyword. Assuming your module is called "test.py", this setup.py will work
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="test",
version="0.0.1",
author="",
author_email="",
description="",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/sampleproject",
py_modules = ["test"],
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
If you think this will expand to multiple modules, then you can go back to using
setuptools.find_packages(). In this case, you want a subdirectory named after your desired package and put the init in there
some_random_project_file
+-- setup.py
README.md
LICENCE
+-- test
+-- __init__.py
test.py

How to make pip/setup.py build extensions (build_ext) when installing from an sdist?

I am trying to create an sdist which I can upload to PyPI and which can then be pip installed successfully by users. The project in question is https://github.com/ratt-ru/CubiCal, and I already have an sdist uploaded to PyPI (https://pypi.org/project/cubical/). Documentation is here.
This project makes use of Cython. According to Cython's documentation, the correct way to distribute Cython code is to bundle the .c and .cpp files generated by cythonize, and I am currently doing so.
The problem is that both pip install cubical and pip install path/to/sdist install the package, but do not correctly build and link extensions. That is, no .so files are generated and consequently the installed package cannot import its Cython modules. I know that the setup.py script works when building from source (e.g. git cloning the repo, cythonizing the extensions, and running pip install -e CubiCal/).
In summary, how do I change my setup.py script to ensure that extensions are handled correctly when installing from an sdist?
Setup.py:
import os
import sys
import glob
import cubical
from setuptools import setup, find_packages
from setuptools.extension import Extension
from setuptools.command.build_ext import build_ext
from setuptools import Command
with open('README.md') as f:
long_description = f.read()
# Try get location of numpy headers. Compilation requires these headers.
try:
import numpy as np
except ImportError:
include_path = ''
else:
include_path = np.get_include()
# Use Cython if available.
try:
from Cython.Build import cythonize
import Cython.Compiler.Options as CCO
except ImportError:
cythonize = None
cmpl_args = ['-ffast-math',
'-O2',
'-march=native',
'-mtune=native',
'-ftree-vectorize' ]
cmpl_args_omp = cmpl_args + ['-fopenmp']
link_args = []
link_args_omp = link_args + ['-lgomp']
# which extensions need to compile through C++ rather than C
cpp_extensions = "cytf_plane", "cyf_slope", "cyt_slope", "rebinning"
class gocythonize(Command):
""" Cythonise CubiCal kernels. """
description = 'Cythonise CubiCal kernels.'
user_options = [('force', 'f', 'Force cythonisation.'),]
def initialize_options(self):
pass
def finalize_options(self):
self.force = self.force or 0
def run(self):
CCO.buffer_max_dims = 9
extensions = []
for source in glob.glob("cubical/kernels/*.pyx"):
name, ext = os.path.splitext(source)
omp = name.endswith("_omp")
# identify which kernels need to go via the C++ compiler
cpp = any([x in name for x in cpp_extensions])
extensions.append(
Extension(name.replace("/","."), [source],
include_dirs=[include_path],
extra_compile_args=cmpl_args_omp if omp else cmpl_args,
extra_link_args=link_args_omp if omp else link_args,
language="c++" if cpp else "c"))
cythonize(extensions, compiler_directives={'binding': True}, annotate=True, force=self.force)
extensions = []
for source in glob.glob("cubical/kernels/*.pyx"):
name, _ = os.path.splitext(source)
is_cpp = any([s in name for s in cpp_extensions])
is_omp = name.endswith("_omp")
extensions.append(
Extension(name.replace("/","."), [name + ".cpp" if is_cpp else name + ".c"],
include_dirs=[include_path],
extra_compile_args=cmpl_args_omp if is_omp else cmpl_args,
extra_link_args=link_args_omp if is_omp else link_args))
# Check for readthedocs environment variable.
on_rtd = os.environ.get('READTHEDOCS') == 'True'
if on_rtd:
requirements = ['numpy',
'cython',
'futures',
'matplotlib',
'scipy']
else:
requirements = ['numpy',
'futures',
'python-casacore>=2.1.2',
'sharedarray',
'matplotlib',
'cython',
'scipy',
'astro-tigger-lsm']
setup(name='cubical',
version=cubical.VERSION,
description='Fast calibration implementation exploiting complex optimisation.',
url='https://github.com/ratt-ru/CubiCal',
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: POSIX :: Linux",
"Programming Language :: Python",
"Topic :: Scientific/Engineering :: Astronomy"],
author='Jonathan Kenyon',
author_email='jonosken#gmail.com',
license='GNU GPL v3',
long_description=long_description,
long_description_content_type='text/markdown',
cmdclass={'build_ext': build_ext,
'gocythonize': gocythonize},
packages=['cubical',
'cubical.data_handler',
'cubical.machines',
'cubical.tools',
'cubical.kernels',
'cubical.plots',
'cubical.database',
'cubical.madmax'],
install_requires=requirements,
include_package_data=True,
zip_safe=False,
ext_modules = extensions,
entry_points={'console_scripts': ['gocubical = cubical.main:main']},
)

How to package the project using wheel in python

I am not aware about wheel. I have requirement.txt file. There is one more thing - wheel. I confused about wheel and requirement.txt. I want package my project using wheel.How I can package my project using wheel so I can easy install all the project dependencies using one shot.
You can use my git to make a new project with setup.py file and after that run
pip install -e .
to make a new version of your project
https://github.com/adouani/create_template
EDIT
Example:
# -*- coding: utf-8 -*-
import os
from setuptools import setup, find_packages
here = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(here, 'README.txt')) as f:
README = f.read()
with open(os.path.join(here, 'CHANGES.txt')) as f:
CHANGES = f.read()
with open(os.path.join(here, 'external_requirements.txt')) as f:
requires = f.readlines()
# on sépare la définition des dépendances internes des dépendances externes
requires.extend([
......
])
setup(
name='..........',
version='0.1.0.dev0',
description='',
long_description=README + '\n\n' + CHANGES,
classifiers=[
"Programming Language :: Python",
"Framework :: Pyramid",
"Topic :: Internet :: WWW/HTTP",
"Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
],
author='............',
author_email='',
url='',
keywords='web wsgi bfg pylons pyramid',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
install_requires=requires,
message_extractors={'.': [
('ihm/**.py', 'lingua_python', None),
('ihm/**.pt', 'lingua_xml', None),
('ihm/**.html', 'html', None),
('ihm/**.py', 'python', None),
('ihm/**.js', 'js', None),
]},
dependency_links=[
'............',
'.............',
'git+http://............#egg=ihm-0.7.0',
],
)

Categories

Resources