Python: Difference between revisions
Adisbladis (talk | contribs) Remove section mixing packages from nixpkgs & pip. This is extremely bad advice that should be avoided, not encouraged through docs. This kind of hacks leaves you with broken envs that cannot be reproduced. Tags: Manual revert Visual edit |
Improve the how to package Python application section about projects that use setup.py |
||
| Line 255: | Line 255: | ||
== Package a Python application == | == Package a Python application == | ||
=== With setup.py === | |||
To package a Python application that uses <code>setup.py</code> you can use <code>buildPythonApplication</code>. More details about this and similar functions can be found in [https://nixos.org/manual/nixpkgs/stable/#building-packages-and-applications the nixpkgs manual]. | |||
For example, we can package this simple flask server <code>main.py:</code> | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
#!/usr/bin/env python | #!/usr/bin/env python | ||
from flask import Flask | from flask import Flask | ||
app = Flask(__name__) | app = Flask(__name__) | ||
| Line 272: | Line 275: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
We also need a <code>setup.py</code> file, like this: | |||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
from setuptools import setup, find_packages | from setuptools import setup, find_packages | ||
setup(name=' | setup(name='myFlaskServer', | ||
version='1.0', | version='1.0', | ||
# Modules to import from other scripts: | # Modules to import from other scripts: | ||
packages=find_packages(), | packages=find_packages(), | ||
# Executables | # Executables | ||
scripts=[" | scripts=["main.py"], | ||
) | ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Then, we use the <code>buildPythonApplication</code> in the <code>default.nix</code>: | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { pkgs ? import <nixpkgs> {} }: | ||
buildPythonApplication { | pkgs.python3Packages.buildPythonApplication { | ||
pname = " | pname = "myFlaskApp"; | ||
version = "1.0"; | version = "0.1.0"; | ||
propagatedBuildInputs = [ flask ]; | propagatedBuildInputs = with pkgs.python3Packages; [ | ||
flask | |||
]; | |||
src = ./.; | src = ./.; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight>Finally, build your project using <code>nix-build</code>. The result will be executable in <code>./result/bin/app.py</code>. | ||
< | |||
</ | |||
== Nixpkgs Python contribution guidelines == | == Nixpkgs Python contribution guidelines == | ||