# On the fly python imports using nixpkgs

**URL:** https://discourse.nixos.org/t/on-the-fly-python-imports-using-nixpkgs/2640
**Category:** Help
**Created:** [April 10, 2019, 1:40pm UTC](https://discourse.nixos.org/t/on-the-fly-python-imports-using-nixpkgs/2640 "2019-04-10T13:40:02Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![costrouc](https://discourse.nixos.org/user_avatar/discourse.nixos.org/costrouc/32/367_2.png) [@costrouc](https://discourse.nixos.org/u/costrouc)
#### Post date: [April 10, 2019, 1:40pm UTC](https://discourse.nixos.org/t/on-the-fly-python-imports-using-nixpkgs/2640/1 "2019-04-10T13:40:02Z")

</div>

_edit_ thanks @adisbladis

Check out this project if you want to import arbitrary python packages on the fly [GitHub - t184256/nixpkgs-python-importer: Violate Nix philosophy, install Python packages mid-session with `from nixpkgs.scipy import scipy`. · GitHub](https://github.com/t184256/nixpkgs-python-importer)

usage for now until in unstable

```shell
nix-shell -I 'nixpkgs=https://github.com/NixOS/nixpkgs/archive/master.tar.gz' -p 'python3.withPackages(ps: with ps; [ps.nixpkgs ps.ipython])' --run 'ipython'

```

if you are looking at this much later use

```auto
nix-shell -p 'python3.withPackages(ps: with ps; [ps.nixpkgs ps.ipython])' --run 'ipython'

```

# My Question

So crazy idea here of something I have wanted in python (and want to implement) that I feel that nixpkgs may be able to make possible. Suppose we have a python with no packages installed only stdlib. I would like to write the following and override the import statement such that it calls nixpkgs to download the package if it does not exist. Completely fine with partial answers and solutions that are hacks.

```python
import numpy as np

np.array([1, 2, 3, 4])

```

In python it is possible to add [import hooks](https://docs.python.org/3/reference/import.html#import-hooks) to the “import” statement. It would try to import numpy and return an import error which would be caught and it would be fed off to call nixpkgs to add numpy to the environment via PYTHONPATH `python37Packages.numpy` for example.

Is something like this possible? I realize that in order for this to work the PYTHONPATH and PATH environment variables but not sure if others as well.

---

<div class="post-metadata">

### Author: ![roni](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@roni](https://discourse.nixos.org/u/roni)
#### Post date: [April 10, 2019, 2:25pm UTC](https://discourse.nixos.org/t/on-the-fly-python-imports-using-nixpkgs/2640/2 "2019-04-10T14:25:50Z")

</div>

This is a neat idea, but rather than hooking into `nix-env` to install missing packages automatically, I think it might be better to arrange for the import hooks to dump a list of the missing Python modules to stdout, or even auto-generate an apprpirate shell.nix file. I say this because using something like a Nix shell is the canonical way to work with Python, and asking the script in question to help generate the list of Python dependencies would be a pretty neat trick.

---

<div class="post-metadata">

### Author: ![adisbladis](https://discourse.nixos.org/user_avatar/discourse.nixos.org/adisbladis/32/77_2.png) [@adisbladis](https://discourse.nixos.org/u/adisbladis)
#### Post date: [April 10, 2019, 3:38pm UTC](https://discourse.nixos.org/t/on-the-fly-python-imports-using-nixpkgs/2640/3 "2019-04-10T15:38:33Z")

</div>

Not only is this possible, it already exists and is in nixpkgs!  
[https://github.com/t184256/nixpkgs-python-importer](https://github.com/t184256/nixpkgs-python-importer)

---

<div class="post-metadata">

### Author: ![adisbladis](https://discourse.nixos.org/user_avatar/discourse.nixos.org/adisbladis/32/77_2.png) [@adisbladis](https://discourse.nixos.org/u/adisbladis)
#### Post date: [April 10, 2019, 4:50pm UTC](https://discourse.nixos.org/t/on-the-fly-python-imports-using-nixpkgs/2640/4 "2019-04-10T16:50:14Z")

</div>

It turns out `nixpkgs-python-importer` was broken on master (and 19.03).  
I’ve fixed and backported the changes required.

In case anyone wants to try this out here is a nice one-liner: `nix-shell -I 'nixpkgs=https://github.com/NixOS/nixpkgs/archive/master.tar.gz' -p 'python3.withPackages(ps: with ps; [ps.nixpkgs ps.ipython])' --run 'ipython'`

You can then try to import a module like so: `from nixpkgs.numpy import numpy as np`

---

<div class="post-metadata">

### Author: ![costrouc](https://discourse.nixos.org/user_avatar/discourse.nixos.org/costrouc/32/367_2.png) [@costrouc](https://discourse.nixos.org/u/costrouc)
#### Post date: [April 10, 2019, 5:09pm UTC](https://discourse.nixos.org/t/on-the-fly-python-imports-using-nixpkgs/2640/5 "2019-04-10T17:09:52Z")

</div>

Thank you for fixing this. Just want to say this is probably one of the most underrated packages! Wow it just works I am going to have to share this with others

---

<div class="post-metadata">

### Author: ![t184256](https://discourse.nixos.org/user_avatar/discourse.nixos.org/t184256/32/1038_2.png) [@t184256](https://discourse.nixos.org/u/t184256)
#### Post date: [April 11, 2019, 1:41am UTC](https://discourse.nixos.org/t/on-the-fly-python-imports-using-nixpkgs/2640/6 "2019-04-11T01:41:38Z")

</div>

> [@costrouc](#):
>
> Just want to say this is probably one of the most underrated packages! Wow it just works I am going to have to share this with others

At some point I considered mentioning it in the Nixpkgs manual, but it is so much against the Nix spirit that I decided against that. Not sure what would be the proper way to promote it.
