This is a result from python.section.md and it has a lack of full explanation. For me its kinda reminder how to start.
- Global installation of Python in
configuration.nix
inenvironment.systemPackages
with e.g.(python36.withPackages(ps: with ps; [ numpy ]))
here i chose python 3.6 any ohter version should work that way. - Create a nix expression like
python.nix
with all dependencies for Python:
with import <nixpkgs> {};
(python35.withPackages (ps: [ps.numpy ps.toolz])).env
- Set up temporary Python environment with nix-shell:
$ nix-shell python.nix
- Run/execute any python script in nix-shell:
$ nix-shell python.nix --run "python3 mypyscript.py"
Or try this as an alternative concept:
- imperative pip in a virtualenv like in other OS. default.nix:
with import <nixpkgs> {};
let
pythonEnv = python37.withPackages (ps: [
ps.virtualenv
ps.pip
]);
in mkShell {
buildInputs = [
pythonEnv
hello
];
shellHook = ''
virtualenv --no-setuptools venv
export PATH=$PWD/venv/bin:$PATH
export PYTHONPATH=venv/lib/python3.7/site-packages/:$PYTHONPATH
pip install -r requirements.txt
'';
}
1.1 or just default.nix:
with import <nixpkgs> {};
with python37Packages;
stdenv.mkDerivation {
name = "gmailenv";
src = null;
buildInputs = [
python37Full
python37Packages.virtualenv
python37Packages.pip
];
shellHook = ''
virtualenv --no-setuptools venv
export PATH=$PWD/venv/bin:$PATH
export PYTHONPATH=venv/lib/python3.7/site-packages/:$PYTHONPATH
pip install -r requirements.txt
'';
}
- run it
$ nix-shell default.nix
- In an optional
requirements.txt
file list whatever python modules e.g. matplotlib, pandas - Therefore you could look for packages e.g. numpy
$ nix-env -qa '.*numpy.*'
- Exit
$ nix-shell
like Ctrl+D or type$ exit
. - Now u should be ready to have an easy imperativ venv with pip
- After you run
$ nix-shell default.nix
runvirtualenv venv
source venv/bin/activate
- if you want to leave
deactivate
followed byCtrl + D
) <- no need, you should be in your virtualenv after starting the shell.