Packaging/Python: Difference between revisions

imported>Adisbladis
Klinger (talk | contribs)
 
(14 intermediate revisions by 9 users not shown)
Line 14: Line 14:
</syntaxHighlight>
</syntaxHighlight>
You can now run <code>nix-shell</code> and it will drop you in a shell similar to the <code>python setup.py develop</code> mode which uses the local code in <tt>./path/to/source</tt> as input. <code>propagatedBuildInputs</code> will contain the packages you  need in your project.
You can now run <code>nix-shell</code> and it will drop you in a shell similar to the <code>python setup.py develop</code> mode which uses the local code in <tt>./path/to/source</tt> as input. <code>propagatedBuildInputs</code> will contain the packages you  need in your project.
After you've finished developing you can replace the relative path with <code>fetchFromGitHub { ... }</code> or <code>[https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/interpreters/python/fetchpypi.nix fetchPypi] { ... }</code>.
After you've finished developing you can replace the relative path with <code>fetchFromGitHub { ... }</code> or <code>[https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/fetchpypi/default.nix fetchPypi] { ... }</code>.


== Pip and Virtualenv enabled nix-shell ==
== Pip and Virtualenv enabled nix-shell ==
Line 144: Line 144:


... or use <code>fetchurl</code> to download the <code>*.whl</code> file directly.<br>
... or use <code>fetchurl</code> to download the <code>*.whl</code> file directly.<br>
examples: [https://grep.app/search?filter%5Brepo%5D%5B0%5D=NixOS/nixpkgs&filter%5Blang%5D%5B0%5D=Nix&filter%5Bpath%5D%5B0%5D=pkgs/development/python-modules/&q=.whl grep for ".whl" in nixpkgs/pkgs/development/python-modules/]


reference: [https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/interpreters/python/fetchpypi.nix fetchPypi implementation]
reference: [https://github.com/NixOS/nixpkgs/blob/master/pkgs/build-support/fetchpypi/default.nix fetchPypi implementation]


== setup.py ==
== Fix Missing <code>setup.py</code> ==


The <code>setup.py</code> file is required for <code>buildPythonPackage</code>,
The <code>setup.py</code> file is required for <code>buildPythonPackage</code>,
but it's missing in some packages
but it's missing in some packages.
If you get the following error, you need to one of the workarounds below.


<pre>
<pre>
Line 166: Line 166:


If both <code>setup.py</code> and <code>pyproject.toml</code> are missing,
If both <code>setup.py</code> and <code>pyproject.toml</code> are missing,
you have to add one of these files, for example:
you have to add one of these files.
 
'''Note:''' sometimes you will be able to find <code>pyproject.toml</code> in the source for a package despite it not being present in a <code>.whl</code> file. You can inspect the contents of a <code>.whl</code> file by downloading it from PyPi and then extracting it with <code>nix-shell -p python311Packages.wheel --command wheel unpack path/to/package.whl</code>.
 
For example, you can create the <code>setup.py</code> in the <code>preBuild</code> phase.


<syntaxHighlight lang=nix>
<syntaxHighlight lang=nix>
buildPythonPackage {
buildPythonPackage {
   preBuild = ''
   preBuild = ''
     cat >setup.py <<'EOF'
     cat > setup.py << EOF
    from setuptools import setup
    setup(
      name='someprogram',
      # ...
    )
    EOF
  '';
}
</syntaxHighlight>
 
setup.py example:
 
<syntaxHighlight lang=python>
from setuptools import setup
from setuptools import setup


Line 205: Line 196:
   },
   },
)
)
    EOF
  '';
}
</syntaxHighlight>
</syntaxHighlight>
More info about the <code>setup.py</code> can be found [https://docs.python.org/3.11/distutils/setupscript.html here]. (<b>note:</b> from python 3.12 onwards, distutils is deprecated see https://docs.python.org/3.11/distutils/index.html)


<code>scripts</code> is useful for self-contained python scripts with no local imports.
<code>scripts</code> is useful for self-contained python scripts with no local imports.
Line 239: Line 235:


== Automatic packaging ==
== Automatic packaging ==
 
{| class="wikitable"
TODO https://github.com/nix-community/pip2nix
|+
 
!
TODO https://github.com/nix-community/poetry2nix
!Project
 
!URL
TODO https://pypi.org/project/pypi2nix/
!Stars
!Status
|-
|TODO
|poetry2nix
|https://github.com/nix-community/poetry2nix
|884+
|unmaintained
|-
|TODO
|pip2nix
|https://github.com/nix-community/pip2nix
|175+
|
|-
|TODO
|<s>pypi2nix</s>
|https://github.com/nix-community/pypi2nix
|194
|archived
|}


== Testing via this command is deprecated ==
== Testing via this command is deprecated ==
Line 264: Line 280:
   checkPhase = ''
   checkPhase = ''
     runHook preCheck
     runHook preCheck
     ${python.interpreter} -m unittest
     ${python3.interpreter} -m unittest
     runHook postCheck
     runHook postCheck
   '';
   '';
Line 272: Line 288:


* [https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/interpreters/python/mk-python-derivation.nix buildPythonPackage implementation]
* [https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/interpreters/python/mk-python-derivation.nix buildPythonPackage implementation]
* [https://ryantm.github.io/nixpkgs/languages-frameworks/python/ Python] in the nixpkgs manual
* [https://nixos.org/manual/nixpkgs/#python Python] in the nixpkgs manual
* [https://github.com/on-nix/python Python on Nix] is an "Extensive collection of Python projects from PyPI"
* [https://github.com/on-nix/python Python on Nix] is an "Extensive collection of Python projects from PyPI"
* [https://ryantm.github.io/nixpkgs/languages-frameworks/rust/#examples Rust section of Nixpkgs manual] - build Rust code in Python projects
* [https://nixos.org/manual/nixpkgs/stable/#examples Rust section of Nixpkgs manual] - build Rust code in Python projects


== References ==
== References ==
[[Category:Python]]