From mboxrd@z Thu Jan 1 00:00:00 1970 From: Hartmut Goebel Subject: Re: PYTHONPATH woes Date: Fri, 23 Feb 2018 13:36:17 +0100 Message-ID: References: <87371tqbyb.fsf@elephly.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------ADBA27A3DE95F693F5DB7EB7" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:49895) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1epCaU-0004mf-SE for guix-devel@gnu.org; Fri, 23 Feb 2018 07:36:31 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1epCaP-0000PR-S8 for guix-devel@gnu.org; Fri, 23 Feb 2018 07:36:30 -0500 Received: from mail-out.m-online.net ([212.18.0.9]:40099) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1epCaP-0000LP-Hl for guix-devel@gnu.org; Fri, 23 Feb 2018 07:36:25 -0500 In-Reply-To: <87371tqbyb.fsf@elephly.net> Content-Language: en-US List-Id: "Development of GNU Guix and the GNU System distribution." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org Sender: "Guix-devel" To: Ricardo Wurmus Cc: guix-devel@gnu.org This is a multi-part message in MIME format. --------------ADBA27A3DE95F693F5DB7EB7 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Am 22.02.2018 um 16:30 schrieb Ricardo Wurmus: > (This wouldn=E2=80=99t help us much for wrapper scripts, though.) Attached please fins an approach for solving this issue. The basic idea is make the application/script use a python within a virtual environment. I nee to rethink some details and check whether this will work out. See the comments in the code for how it is intended to work. --=20 Regards Hartmut Goebel | Hartmut Goebel | h.goebel@crazy-compilers.com | | www.crazy-compilers.com | compilers which you thought are impossible | --------------ADBA27A3DE95F693F5DB7EB7 Content-Type: text/x-scheme; name="test-new-build2.scm" Content-Disposition: attachment; filename="test-new-build2.scm" Content-Transfer-Encoding: quoted-printable ;;; Copyright =C2=A9 2018 Hartmut Goebel (use-modules (guix) (gnu packages python) (guix licenses)) ;; TODO decide which path to use (define VENV-DIR "/share/guix-venv/") ; trailing slash! ;; python-uniseg is a reasonable small package without any dependencies a= nd ;; including a script (package (inherit python-uniseg) (name "my-python-uniseg") (version (package-version python-uniseg)) (arguments `(#:tests? #f ; copied from base package #:phases (modify-phases %standard-phases (replace 'wrap (lambda* (#:key inputs outputs #:allow-other-keys) ;; This sets up a small virtual environment for the package ;; and wraps the scripts to=20 ;; ../share/guix-venv/my-site-display-0.1 ;; +- pyvenv.cfg ;; +- bin/python ;; +- lib/pythonX.Y/site-packages/NAME.pth (let* ((out (assoc-ref outputs "out")) (venv (string-append out ,VENV-DIR ,name "-" ,version)) (python (assoc-ref inputs "python")) (site-dir (string-append "/lib/python" "3.5" ;; FIXME (get-python-version pyth= on) "/site-packages")) (PYTHONPATH ;; FIXME: Why is ther only one entry? (cons (string-append out site-dir) (search-path-as-string->list (or (getenv "PYTHONPATH") ""))))) (mkdir-p (string-append venv "/bin")) (mkdir-p (string-append venv site-dir)) ;; The existance of a pyvenv.cfg file marks this as being a virtual ;; environment (call-with-output-file (string-append venv "/pyvenv.cfg") (lambda (p) (format p "#include-system-site-packages =3D false"))) ;; Link to all required packages using a .pth file (call-with-output-file (string-append venv site-dir "/" ,name ".pth") (lambda (p) (for-each (lambda (pkg) (format p "~a~%" pkg)) PYTHONPATH))) ;; Create the python "executable" within the virtual environment (for-each (lambda (name) (symlink (string-append python "/bin/" name) (string-append venv "/bin/" name))) `("python" ,"python3")) ))) ;; Re-wrap the scripts to use the python within the virtual env (add-after 'patch-shebangs 'patch-python-venv-shebang (lambda* (#:key outputs inputs #:allow-other-keys) =20 (let* ((out (assoc-ref outputs "out")) (venv (string-append out ,VENV-DIR ,name "-" ,version)) (python (assoc-ref inputs "python"))) (define (venv-program file) (substitute* file (((string-append "#!" python "/bin/python([0-9](\\.[0-9])?= )?")) (string-append "#!" venv "/bin/python")))) ;; FIXME: Use logic from python-build-system to find scripts (venv-program (string-append out "/bin/uniseg-dbpath")) )))))) ;; Some requirement to check if all required pathes are included into t= he ;; .pth-file. (inputs `(("req" ,python-simplejson))) ) --------------ADBA27A3DE95F693F5DB7EB7--