-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python 2.7 Full: sqlite3 module doesn't work in virtualenv #492
Comments
Seems like C compiled module doesn't get propagated. |
IIUC the typical One way to work with this is to install e.g. python27Packages.sqlite3 into a profile and add the site-packages path in that profile to your PYTHONPATH. From there you should be able to use the virtualenv as normal. I think it's handy to put all the c python modules in the nix profile and plain python stuff in the virtualenv. This is something like the setup described here: https://nixos.org/wiki/Using_Profiles_for_Development_Environments @chaoflow has a more advanced setup which can be used to bootstrap and maintain a familiar python development environment: https://github.com/chaoflow/tpv.http/blob/master/Makefile |
What about providing virtualenv a python with all modules available? |
For example, patching virtualenv so that it copies the nix python wrapper instead of the python binary into the virtualenv? The problem with that is it would get garbage collected. If, instead we link to the current version of the wrapped python (e.g. /run/current-system/sw/bin/python) that will change with any update, also potentially breaking the virtualenv. IIUC this is always a problem with using virtualenv on nix without a dedicated profile (or similar). Any dependencies provided by nix may disappear after garbage collection, e.g. libxml for lxml which would break the virtualenv. Maybe you have something else in mind? I guess the motivation is to have virtualenv work on nix in the same way as it would on other distros. After upgrading a conventional distro don't you run into problems with virtualenv too? How does it work on gentoo? Maybe I'm imagining problems that don't exist :) |
My understanding is that if the virtualenv derivation refers to pythonFull -- which it uses to copy that script into generated environments --, then pythonFull would not be garbage collected as long as someone references virtualenv. Or am I missing something? |
Isn't pythonFull the wrapped version of python? So IIUC, you would install virtualenv, then use it to create a virtualenv, which would copy the wrapped python into venv/bin/python then you'd update your system and garbage collect. Nix wouldn't know about your copy of the wrapped python in venv/bin/python, right? So the wrapper would still have the old paths. I guess the best way to find out is to try it, but I don't see how it would work myself. |
Okay, I see your point. I don't understand, though, how use of plain |
How about using myEnvFun to create your environment with pythonFull, you can also install virtualenv and use it in that environment https://nixos.org/wiki/Howto_develop_software_on_nixos? |
|
@chaoflow @cillianderoiste what about putting following at the end of activate script:
|
I think I'd prefer to add the paths to the current profile site-packages directory ~/.nix-profile/lib/python2.7/site-packages/ (including the recursivePthLoader) rather than the /nix/store paths, since that should survive more updates. We should also take care to the right thing inside a myEnv environment. Perhaps we just need to add a default virtualenv.ini or otherwise specify extra-search-dir parameters (if that does what I think it does). https://pypi.python.org/pypi/virtualenv#environment-variables-and-configuration-files |
Yeah, let's put the symlinks from profile (no idea how to do that, but let's see). extra-search-dir only tells virtualenv where to find setuptools/pip eggs (which we should use to achieve better purity), so that's not really an option. From #pypa:
|
Tnx for the workaround. I run into this issue to. |
@iElectric What's the status on this? IMO having references to store paths outside the store (other than (possibly indirect) gcroots) is generally a bad idea. |
Virtualenv is still broken when python updates. We need to fix this somehow. |
Is it urgent enough that we should mark this for the |
Since the problem exists from the very beginning, I don't think it's urgent.
|
I noticed that the following works:
I was thinking about patching the virtualenv generated site.py to add the relevant path(s) to sys.path. It's amazing, but the contents of site.py are actually inline, and compressed: https://github.com/pypa/virtualenv/blob/develop/virtualenv.py#L1847 so that could be tricky. Perhaps modifying it after it gets written out https://github.com/pypa/virtualenv/blob/develop/virtualenv.py#L1193 (using python, patched into the virtualenv.py file via the nix expression) would work, but it's all a bit crazy. The code would need to get the path to the wrapper for the version of python specified e.g. Maybe there's an easier way to tell it to do whatever python normally does with PYTHONHOME. Unless there are better ideas, I'll play around with that a bit. |
There's an easier way. We can get virtualenv to create a sitecustomize.py with:
|
It has to point to the profile, otherwise once the wrapper is gc, it will break again. |
I don't think there's any way around that though. I think the virtualenv needs to be recreated after garbage collection e.g.
I think the best we can do here is to make virtualenv provide the same modules provided by the wrapper by default. EDIT: brevity |
This seems to work:
|
it's an improvement, so +1 |
Cool! I'll do a bit more testing and if it looks OK (and there are no concerns) I'll push it tomorrow. |
Note: simply calling `virtualenv .` will not produce a ./bin/python which can import e.g. sqlite3, using `virtualenv --python=python2.7` will, if python2.7 is python27Full (the wrapped python). I'm not sure if this is a bug or a feature.
I'll close this since I believe the initial issue is solved. We could create a new ticket to tackle the issue of virtualenvs surviving garbage collection, but I'm not sure there is a nice way. At any rate, we should document the situation and encourage people to use nix instead of virtualenv where possible and otherwise to recreate their virtualenvs after garbage collection. |
Good job! |
I'm getting this with just nix-shell and straight nix python (with no virtualenv). Should I open a separate ticket? let
pkgs = import <nixpkgs> {};
in
{ stdenv ? pkgs.stdenv }:
with pkgs; stdenv.mkDerivation {
name = "python-nix";
version = "0.1.0.0";
src = ./.;
buildInputs = [ python nixops sqlite ];
} results in an environment that can't |
Oh never mind, I needed |
a virtualenv created useing `virtualenv venv` will not be able to import modules such as sqlite3: NixOS/nixpkgs#492 In order for virtualenv wrapped python to do so: ``` $ virtualenv --python=`which python2.7` venv ```
This also affects the tox test runner (which uses an unpatched version of virtualenv internally). This means that it is not possible to execute python2.7 tests for a django project/app because of the missing _sqlite3 module. |
I think we should include ncurses and sqlite by default. They're both very light and convenient. |
@jgeerds could you show an example of how you use the tox test runner with Nix? |
Since #19309 we don't have separate modules anymore except |
@chaoflow @garbas can you reproduce?
The text was updated successfully, but these errors were encountered: