Building python package with data files and using them - setuptools

I'd like to build a python package from the following directory:
my_package/
main_funcs.py
extra_funcs.py
data/
data_file.dat
so that extra_funcs.py is used in main_funcs.py, and also gets data from data_file.dat.
I'd like to eventually have a .whl and a .tar.gz files for exporting the package.
How can I do that?
I didnt understand how to create them with setuptools, or how to add non-code files on pip build.
Also, I'm not sure how to call the data files from inside the code after the packing.

Related

Simplest way to prepare a private repo for pip install

I have a private repository that I want to be able to install using pip, like this:
pip install git+https://USER_NAME:APP_PASSWORD#GIT_URL/PATH_TO_YOUR_REPO.git
I tried it, but I got the error message that my repo does not contain a setup.py. I get the idea that I have not created a package. Let's suppose we have a repo with nothing but one python file in it that we want to import (with the command above). I want to take this repo from that state, to one where I can use the above pip install. Let us suppose the one file is called my_python.py What is the bare minimum that I need to add to do that? I have already added the setup.py and it contains the following:
from setuptools import setup
setup(
name='CoreTools',
version='1.0',
description='Core Tools for our software',
author='Guy',
author_email='guy#guy.com',
packages=['CoreTools'], #same as name
install_requires=['wheel', 'bar', 'greek'], #external packages as dependencies
)
I guess I need an __init__.py file. I have discovered that I need a package folder called "CoreTools". I am guessing that the files that I want to use should go in that folder? That means that if I am maintaining this package, I need to edit the files in that folder and commit them there.
I am using PyCharm and it gives the option to create a package and so I tried this. I creates a new folder with an __init__.py in it. Where does setup.py go? Does it go in the main folder or in the new package folder with __init__.py?

Is there a simple way to convert setup.py to pyproject.toml

We have multiple python projects, and are considering converting them to use pyproject.toml instead of setup.py.
Is there a simple way to automate this?
While dealing with some pyproject.toml issues, I encountered this project:
https://pypi.org/project/ini2toml/
The projects description:
This project is experimental and under active development Issue reports and contributions are very welcome.
The original purpose of this project is to help migrating setup.cfg files to PEP 621, but by extension it can also be used to convert any compatible .ini/.cfg file to .toml.
While this only helps to turn .cfg/.ini files to PEP 621 .toml, there is another project that turn setup.py files into cfg files.
https://github.com/gvalkov/setuptools-py2cfg
This script helps convert existing setup.py files to setup.cfg in the format expected by setuptools.
Combining these two processes by writing a script you could potentially come up with an automated way to transform the files. I have not tried this as of yet but would be interested if you could make this idea work :)
pdm supports importing metadata from various existing and older metadata files, including setup.py.
You can either:
run pdm init on your project root (with the old metadata files) and follow the instructions, or
run pdm import setup.py explicitly.
See Import project metadata from existing project files for more details.

Why does python setup.py bdist_wheel creates a build folder?

I just learned to upload my own python packages to PyPI thanks to this amazing tutorial. I am trying now to better understand how wheels works and I found this article helpful.
However, I still do not understand why python setup.py bdist_wheel creates an almost empty directory named build with two subfolders: bdist.win-amd64 (empty) and lib (which contains a copy of my package), in addition to the .whl file in the dist directory that developers will later upload to PyPI by doing python -m twine upload dist/*.
Why is this build directory necessary? I mean, would the dist directory not be enough? Moreover, why is the .whl called a binary distribution if the code is not actually compiled.
python setup.py bdist_wheel internally runs python setup.py install which in turn runs python setup.py build which compiles/builds the project into a temporary location inside build/ directory and then installs compiled project into another temporary location inside build/ directory. From files in that second temporary location it creates a wheel.
As for the compilation — python modules could be written in C/C++ and often they are. So python setup.py build needs to compile. If there is nothing to compile — well, the compilation step is skipped but the build step is still run.

How to Build an RPM from Source in One Line without Spec Files etc?

I've been reading about building RPMs, and the process is quite complex. Is there any program/software that works like this:
Download tar.gz file. Extract to directory
cd into directory
Run
RPM file is output into the directory
Does any such program exist? It seems as if it should. After all, when I run make, make install etc, I don't need to specify spec files, provide locations for where the software has to be installed. So why should I have to do all that for creating RPMs?
I've tried using checkinstall, but I keep getting errors like "Directory not found: /root/rpmbuild/BUILDROOT/hello-2.10-1.x86_64/usr"
So is there an easier way?
No. There is no easier way.
Sometimes upstream provide 'make rpm' target. Sometime checkinstall works. But often you have to create the spec file manually.
BTW that error from checkinstall reveals two things:
you are running that command as root. That is very very unwise.
you should create few build directories. Run command rpmdev-setuptree it will create them for you.

Expanding a NuGet Package (nupkg) From the Command-Line

I have access to the NuGet command-line tool but not to any unzip tools. Is it possible to install a package already saved locally?
After doing a number of searches, I've come up with nothing that handles such a simple requirement. I've also looked at the code but it's far too lengthy/dense to quickly come to a definite conclusion, though, by all indications, it doesn't support the direct install of a local file.
Since it's really just a zip file.
mv ~/path/to/package.nupkg package.zip
This will change the file extension to be a zip file. Then:
unzip package.zip -d ~/Output/dir
If you have the .nupkg stored locally in a directory and all you want to do is extract the files you could use NuGet.exe and do something like:
NuGet.exe install -o extract-directory MyPackageId -source /Full/Path/To/Directory/Containing/NuGet/Package/NuPkgs
The -source parameter allows you to define a new source. In this case the directory where the .nupkg file exists.
The -o parameter defines the directory where you want the NuGet package extracted to.
The above seems to work on the Mac with NuGet 2.12.