Packaging/Python: Difference between revisions

imported>Milahu
fix: python -m unittest
imported>Milahu
setup.py: add `format = "pyproject";`, wording
Line 163: Line 163:
The <code>setup.py</code> file is required by <code>buildPythonPackage</code>,
The <code>setup.py</code> file is required by <code>buildPythonPackage</code>,
but it's missing in some packages
but it's missing in some packages
<pre>
FileNotFoundError: [Errno 2] No such file or directory: 'setup.py'
</pre>
If the package has a <code>pyproject.toml</code> file, set
<syntaxHighlight lang=nix>
buildPythonPackage {
  format = "pyproject";
}
</syntaxHighlight>
If both <code>setup.py</code> and <code>pyproject.toml</code> are missing,
you have to add one of these files, for example:
<syntaxHighlight lang=nix>
buildPythonPackage {
  preBuild = ''
    cat >setup.py <<'EOF'
    from setuptools import setup
    setup(
      name='someprogram',
      # ...
    )
    EOF
  '';
}
</syntaxHighlight>


setup.py example:
setup.py example:
Line 183: Line 212:
   ],
   ],
   entry_points={
   entry_points={
    # example: file some_module.py -> function main
     #'console_scripts': ['someprogram=some_module:main']
     #'console_scripts': ['someprogram=some_module:main']
   },
   },
Line 188: Line 218:
</syntaxHighlight>
</syntaxHighlight>


In this example, <code>someprogram.py</code> would be installed as <code>$out/bin/someprogram.py</code>
<code>scripts</code> is useful for self-contained python scripts with no local imports.
 
If a python script has local imports,
for example <code>from .some_module import some_function</code>,
either include all files in the <code>scripts</code> array,
or add only the entry function to <code>entry_points</code>.


In this example, <code>someprogram.py</code> would be installed as <code>$out/bin/someprogram.py</code>.<br>
To rename the binary, for example to remove the <code>.py</code> file extension, you can use <code>postInstall</code>
To rename the binary, for example to remove the <code>.py</code> file extension, you can use <code>postInstall</code>