Pygame installed in virtualenv won't work

For a development project, I need the Beta for pygame2. I tried to install it using virtualenv, which works, but at runtime I get the following error:

$ python
Python 3.7.6 (default, Dec 18 2019, 19:23:55) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
pygame 2.0.0.dev8 (SDL 2.0.12, python 3.7.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> pygame.display.init()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pygame.error: No available video device
>>> 

I assume it has something to do with SDL2 not finding the x libraries or something like this, but I don’t know how to tackle this problem properly. I also tried it with pygame1.9.6 with the same result.

@AberDerBart - did you ever solve this issue?

I’m now facing the same situation trying to setup a development environment and am curious how to let SDL2 find the libraries it needs.

I installed pygame via shell.nix and used pip for the rest:

let 
  unstable = import <nixos-unstable> {}; 
in pkgs.mkShell {
  buildInputs = [ 
    unstable.python3
    unstable.python3Packages.pip
    unstable.python3Packages.pygame
  ];  
  shellHook = ''
            alias pip="PIP_PREFIX='$(pwd)/_build/pip_packages' \pip"
            export PYTHONPATH="$(pwd)/_build/pip_packages/lib/python3.7/site-packages:$PYTHONPATH"
            unset SOURCE_DATE_EPOCH
  '';
}

It’s not the prettiest solution, but it worked for me. It would probably be better to install all packages via shell.nix

Thanks - I ended up using the packaged version, too, though I was able to use mach-nix to keep everything declarative (so far…I’m still working to get ray[rllib] working).

{ pkgs ? import <nixpkgs> {} }:
let
    mach-nix = import (
        builtins.fetchGit {
            url = "https://github.com/DavHau/mach-nix/";
            ref = "refs/tags/3.3.0";
        }
    ) {};
    custom-python = mach-nix.mkPython {
        python = "python38Full";
        requirements = ''
            griddly
            matplotlib
        '';
        providers = {
            _default = "wheel,sdist,nixpkgs";
        };
    };
in
with pkgs;
mkShell {
  buildInputs = [
    custom-python
    python38Packages.pygame # Nixpkgs fixes up some complexities
    python38Packages.pytorch # Nixpkgs fixes up some complexities
    vulkan-loader # Griddly needs the Vulkan-SDK
  ];
  LD_LIBRARY_PATH="${vulkan-loader}/lib";
}