doc: python: fixing mistake in venv example

When updating the section to python 3 some places still
referred to pythonPackages and were overlooked.
Decided to switch it to be more similar to the first
example binding pythonPackages and clarified comments a
bit based on confusion I observed on IRC.

Related to https://github.com/NixOS/nixpkgs/pull/77569
This commit is contained in:
Dima 2020-01-31 12:51:39 +01:00 committed by Jon
parent c2d2c2d0ca
commit e9ba4b94fb

View file

@ -1061,11 +1061,9 @@ in pkgs.mkShell rec {
pythonPackages.numpy pythonPackages.numpy
pythonPackages.requests pythonPackages.requests
# the following packages are related to the dependencies of your python # In this particular example, in order to compile any binary extensions they may
# project. # require, the python modules listed in the hypothetical requirements.txt need
# In this particular example the python modules listed in the # the following packages to be installed locally:
# requirements.txt require the following packages to be installed locally
# in order to compile any binary extensions they may require.
taglib taglib
openssl openssl
git git
@ -1075,7 +1073,8 @@ in pkgs.mkShell rec {
zlib zlib
]; ];
# Now we can execute any commands within the virtual environment # Now we can execute any commands within the virtual environment.
# This is optional and can be left out to run pip manually.
postShellHook = '' postShellHook = ''
pip install -r requirements.txt pip install -r requirements.txt
''; '';
@ -1091,12 +1090,14 @@ with import <nixpkgs> { };
let let
venvDir = "./.venv"; venvDir = "./.venv";
pythonPackages = python3Packages;
in pkgs.mkShell rec { in pkgs.mkShell rec {
name = "impurePythonEnv"; name = "impurePythonEnv";
buildInputs = [ buildInputs = [
python3Packages.python pythonPackages.python
python3Packages.virtualenv # Needed when using python 2.7
... # pythonPackages.virtualenv
# ...
]; ];
# This is very close to how venvShellHook is implemented, but # This is very close to how venvShellHook is implemented, but
@ -1108,14 +1109,18 @@ in pkgs.mkShell rec {
echo "Skipping venv creation, '${venvDir}' already exists" echo "Skipping venv creation, '${venvDir}' already exists"
else else
echo "Creating new venv environment in path: '${venvDir}'" echo "Creating new venv environment in path: '${venvDir}'"
# Note that the module venv was only introduced in python 3, so for 2.7
# this needs to be replaced with a call to virtualenv
${pythonPackages.python.interpreter} -m venv "${venvDir}" ${pythonPackages.python.interpreter} -m venv "${venvDir}"
fi fi
# Under some circumstances it might be necessary to add your virtual # Under some circumstances it might be necessary to add your virtual
# environment to PYTHONPATH, which you can do here too; # environment to PYTHONPATH, which you can do here too;
# PYTHONPATH=$PWD/${venvDir}/${python.sitePackages}/:$PYTHONPATH # PYTHONPATH=$PWD/${venvDir}/${pythonPackages.python.sitePackages}/:$PYTHONPATH
source "${venvDir}/bin/activate" source "${venvDir}/bin/activate"
# As in the previous example, this is optional.
pip install -r requirements.txt pip install -r requirements.txt
''; '';
} }