Create the following default.nix and run nix-shell:
# https://github.com/NixOS/nixpkgs/blob/release-20.03/doc/languages-frameworks/python.section.md#how-to-consume-python-modules-using-pip-in-a-virtual-environment-like-i-am-used-to-on-other-operating-systems
with import <nixpkgs> { };
let
pythonPackages = python3Packages;
in pkgs.mkShell rec {
name = "impurePythonEnv";
venvDir = "./.venv";
buildInputs = [
# A Python interpreter including the 'venv' module is required to bootstrap
# the environment.
pythonPackages.python
# This execute some shell code to initialize a venv in $venvDir before
# dropping into the shell
pythonPackages.venvShellHook
# Those are dependencies that we would like to use from nixpkgs, which will
# add them to PYTHONPATH and thus make them accessible from within the venv.
# pythonPackages.pandas # UNCOMMENT THIS LINE FOR THE WORKAROUND
# In this particular example, in order to compile any binary extensions they may
# require, the Python modules listed in the hypothetical requirements.txt need
# the following packages to be installed locally:
# ...
];
# Now we can execute any commands within the virtual environment.
# This is optional and can be left out to run pip manually.
postShellHook = ''
# allow pip to install wheels
unset SOURCE_DATE_EPOCH
pip install -r requirements.txt
'';
}
I'm running this on 20.03.
The referenced requirements.txt contains a single line, pandas==1.0.1.
When trying to import pandas, I run into
Traceback (most recent call last):
File "/xxx/xxx/xxx/xxx.py", line 2, in <module>
import pandas as pd
File "/xxx/xxx/xxx/.venv/lib/python3.7/site-packages/pandas/__init__.py", line 54, in <module>
from pandas.core.api import (
File "/xxx/xxx/xxx/.venv/lib/python3.7/site-packages/pandas/core/api.py", line 29, in <module>
from pandas.core.groupby import Grouper, NamedAgg
File "/xxx/xxx/xxx/.venv/lib/python3.7/site-packages/pandas/core/groupby/__init__.py", line 1, in <module>
from pandas.core.groupby.generic import DataFrameGroupBy, NamedAgg, SeriesGroupBy
File "/xxx/xxx/xxx/.venv/lib/python3.7/site-packages/pandas/core/groupby/generic.py", line 60, in <module>
from pandas.core.frame import DataFrame
File "/xxx/xxx/xxx/.venv/lib/python3.7/site-packages/pandas/core/frame.py", line 124, in <module>
from pandas.core.series import Series
File "/xxx/xxx/xxx/.venv/lib/python3.7/site-packages/pandas/core/series.py", line 122, in <module>
class Series(base.IndexOpsMixin, generic.NDFrame):
File "/xxx/xxx/xxx/.venv/lib/python3.7/site-packages/pandas/core/series.py", line 4562, in Series
plot = CachedAccessor("plot", pandas.plotting.PlotAccessor)
AttributeError: module 'pandas' has no attribute 'plotting'
When the line UNCOMMENT THIS LINE is uncommented, it works just fine.
Expected behavior
I expect pandas as installed in a venv to work just as well as the nix system pandas, especially if they're both version 1.0.1.
Additional context
Although I have a workaround, I prefer to understand what could possibly cause this. Although I'd always "just work the Nix way" (i.e. have my dependencies purely defined as references to nix packages), it is not feasible to do this when shifting contexts between many projects that I did not set up myself. In those cases being able to just install from a given set of requirements is crucial.
Describe the bug
When installing Python pandas inside a
venv
as described in the user guide section How to consume python modules using pip in a virtual environment like I am used to on other Operating Systems I run into the errorAttributeError: module 'pandas' has no attribute 'plotting'
To Reproduce
Create the following
default.nix
and runnix-shell
:I'm running this on 20.03.
The referenced
requirements.txt
contains a single line,pandas==1.0.1
.When trying to import pandas, I run into
When the line
UNCOMMENT THIS LINE
is uncommented, it works just fine.Expected behavior
I expect pandas as installed in a
venv
to work just as well as the nix system pandas, especially if they're both version 1.0.1.Additional context
Although I have a workaround, I prefer to understand what could possibly cause this. Although I'd always "just work the Nix way" (i.e. have my dependencies purely defined as references to nix packages), it is not feasible to do this when shifting contexts between many projects that I did not set up myself. In those cases being able to just install from a given set of requirements is crucial.