Skip to main content
Manu Fernandes
Inspiring
Jul 3, 2023 5:31 AM UTC

U2Python - how to install python package into u2python site-packages

hi folks,

I hope someone can help me. 

First, I want to add pypi.org packages /modules and have it available to import on script.py executed from RUNPY or BASIC py function.

How to do it fom within  Universe ? 

Second, I want to distribute our own modules with my basic application. (It's a type19 file (directory) with .py files, each one is a 'module'.)

how to deploy it into u2python ? 

many thanks



------------------------------
Manu Fernandes
------------------------------

    7 replies

    Mike Rajkowski
    Rocketeer
    Jul 3, 2023 11:04 AM UTC

    hi folks,

    I hope someone can help me. 

    First, I want to add pypi.org packages /modules and have it available to import on script.py executed from RUNPY or BASIC py function.

    How to do it fom within  Universe ? 

    Second, I want to distribute our own modules with my basic application. (It's a type19 file (directory) with .py files, each one is a 'module'.)

    how to deploy it into u2python ? 

    many thanks



    ------------------------------
    Manu Fernandes
    ------------------------------

    Manu,

    There are a lot of options to he pip module, and many ways to get all the needed packages loaded, so I will not focus on that but more of the underlying question on how to pip install from a Python program launched from UniVerse.

    You mention that you have a script.py program that will pip install the package. 

    To run from UniVerse, put it into a Directory type file ( like PP in the XDEMO account ), and then do the RUNPY command:

    RUNPY PP script.py

    As for the question on how to deploy your modules, I suggest you go with creating your own .pth file in the UniVerse Python directory ( i.e. same location as the u2.pth that by default would be in C:\\U2\\UV\\python on windows )

    I put a much longer description in another thread which may help

    Yet the other post had to deal with having multiple Python modules  with the same name in the sys.path.

    ------------------------------
    Mike Rajkowski
    MultiValue Product Evangelist
    Rocket Internal - All Brands
    US
    ------------------------------

    Manu Fernandes
    Inspiring
    Jul 5, 2023 5:32 AM UTC

    Manu,

    There are a lot of options to he pip module, and many ways to get all the needed packages loaded, so I will not focus on that but more of the underlying question on how to pip install from a Python program launched from UniVerse.

    You mention that you have a script.py program that will pip install the package. 

    To run from UniVerse, put it into a Directory type file ( like PP in the XDEMO account ), and then do the RUNPY command:

    RUNPY PP script.py

    As for the question on how to deploy your modules, I suggest you go with creating your own .pth file in the UniVerse Python directory ( i.e. same location as the u2.pth that by default would be in C:\\U2\\UV\\python on windows )

    I put a much longer description in another thread which may help

    Yet the other post had to deal with having multiple Python modules  with the same name in the sys.path.

    ------------------------------
    Mike Rajkowski
    MultiValue Product Evangelist
    Rocket Internal - All Brands
    US
    ------------------------------

    hello Mike,

    thank you for the reply.

    so, here my context. 

    I develop a uv app which include his own py module and pypi.org module requirements, listed on a requirement.txt file. 

    at install/upgrade, I'll install all my pypi.org requirements. 

    I suggest launch  pip install -r path\\requirements.txt from basic EXECUTE.  

    what's your advice to run it in correlation with uv/python installation ! 

    (I do not known if the machine run another py env !)

    many thanks,



    ------------------------------
    Manu Fernandes
    ------------------------------
    Manu Fernandes
    Inspiring
    Jul 5, 2023 5:42 AM UTC

    Manu,

    There are a lot of options to he pip module, and many ways to get all the needed packages loaded, so I will not focus on that but more of the underlying question on how to pip install from a Python program launched from UniVerse.

    You mention that you have a script.py program that will pip install the package. 

    To run from UniVerse, put it into a Directory type file ( like PP in the XDEMO account ), and then do the RUNPY command:

    RUNPY PP script.py

    As for the question on how to deploy your modules, I suggest you go with creating your own .pth file in the UniVerse Python directory ( i.e. same location as the u2.pth that by default would be in C:\\U2\\UV\\python on windows )

    I put a much longer description in another thread which may help

    Yet the other post had to deal with having multiple Python modules  with the same name in the sys.path.

    ------------------------------
    Mike Rajkowski
    MultiValue Product Evangelist
    Rocket Internal - All Brands
    US
    ------------------------------

    then, 

    I develop a uv app which include his own py module. 

    I'll define a myapp.pth file into site directory to declare my module's path into sys.path at uvsession start. 

    so, I do this :

    MYMODULEPATH = "accountName.path\\/filename" 
    builtins.py=PyImport('builtins')
    site.py=PyImport('site')
    sitepackages = PyCallMethod(site.py,'getsitepackages')
    sitepackages = PyCallMethod(builtins.py,'str',sitepackages)
    convert ",[]'" to @am in sitepackages 
    loop
        remove sitepack from sitepackages setting eos 
        if upcase(sitepack[13]) = 'SITE-PACKAGES' then 
           sitepack = trim(change(sitepack,'\\\\','\\'))
           openpath sitepack to f.sitepack then 
                  write MYMODULEPATH on f.sitepack,'myapp.pth' 
                  exit 
           end
    while eos do repeat 
    if sitepack = '' then print 'error: u2python site-packages not found'   

     

    I use .../site-packges because on Linux, site does not search sys.prefix but (sys.prefix + "/lib/site-packages").

    Do you have another idea to deploy easily ?  

    many thanks



    ------------------------------
    Manu Fernandes
    ------------------------------
    Mike Rajkowski
    Rocketeer
    Jul 6, 2023 9:47 AM UTC

    hello Mike,

    thank you for the reply.

    so, here my context. 

    I develop a uv app which include his own py module and pypi.org module requirements, listed on a requirement.txt file. 

    at install/upgrade, I'll install all my pypi.org requirements. 

    I suggest launch  pip install -r path\\requirements.txt from basic EXECUTE.  

    what's your advice to run it in correlation with uv/python installation ! 

    (I do not known if the machine run another py env !)

    many thanks,



    ------------------------------
    Manu Fernandes
    ------------------------------

    Manu,

    Running the pip from an basic EXECUTE should work, yet I prefer to launch pip from the python shell.

    i.e.

    python -m pip install module_name

    The reason for this is that if there is a newer version of the module in pypi.org for a different version of python pip may pick up the wrong version ( this is based on how the module was created )

    Either way, I would recommend logging off after installing/upgrading packages from BASIC, since you would have to script a lot more to check if python was in fact running in the U2 process at the time you modified the packages.  



    ------------------------------
    Mike Rajkowski
    MultiValue Product Evangelist
    Rocket Internal - All Brands
    US
    ------------------------------
    Steve Wingfield
    Rocketeer
    Jul 6, 2023 10:19 AM UTC

    hello Mike,

    thank you for the reply.

    so, here my context. 

    I develop a uv app which include his own py module and pypi.org module requirements, listed on a requirement.txt file. 

    at install/upgrade, I'll install all my pypi.org requirements. 

    I suggest launch  pip install -r path\\requirements.txt from basic EXECUTE.  

    what's your advice to run it in correlation with uv/python installation ! 

    (I do not known if the machine run another py env !)

    many thanks,



    ------------------------------
    Manu Fernandes
    ------------------------------

    Hi Manu,

    With respect to the following:

    "... launch  pip install -r path\\requirements.txt... (I do not known if the machine run another py env !)"

    • This should work fine, just make sure to use the full path to the pip program that ships with UV.  For example
      • /usr/uv/python/bin/pip3 install -r path/requirements.txt
      • This way, your modules will end up in the site-packages directory for UV's python installation (and not the site-packages of some other python installation that might be installed on the machine).



    ------------------------------
    Steven Wingfield
    Principal Software Engineer
    Rocket Software
    College Station, TX, USA
    ------------------------------
    Mike Rajkowski
    Rocketeer
    Jul 6, 2023 10:29 AM UTC

    then, 

    I develop a uv app which include his own py module. 

    I'll define a myapp.pth file into site directory to declare my module's path into sys.path at uvsession start. 

    so, I do this :

    MYMODULEPATH = "accountName.path\\/filename" 
    builtins.py=PyImport('builtins')
    site.py=PyImport('site')
    sitepackages = PyCallMethod(site.py,'getsitepackages')
    sitepackages = PyCallMethod(builtins.py,'str',sitepackages)
    convert ",[]'" to @am in sitepackages 
    loop
        remove sitepack from sitepackages setting eos 
        if upcase(sitepack[13]) = 'SITE-PACKAGES' then 
           sitepack = trim(change(sitepack,'\\\\','\\'))
           openpath sitepack to f.sitepack then 
                  write MYMODULEPATH on f.sitepack,'myapp.pth' 
                  exit 
           end
    while eos do repeat 
    if sitepack = '' then print 'error: u2python site-packages not found'   

     

    I use .../site-packges because on Linux, site does not search sys.prefix but (sys.prefix + "/lib/site-packages").

    Do you have another idea to deploy easily ?  

    many thanks



    ------------------------------
    Manu Fernandes
    ------------------------------

    Manu,

    That is as good of a solution as any.  Especially for those who want to do this from BASIC.



    ------------------------------
    Mike Rajkowski
    MultiValue Product Evangelist
    Rocket Internal - All Brands
    US
    ------------------------------
    Steve Wingfield
    Rocketeer
    Jul 6, 2023 2:45 PM UTC

    then, 

    I develop a uv app which include his own py module. 

    I'll define a myapp.pth file into site directory to declare my module's path into sys.path at uvsession start. 

    so, I do this :

    MYMODULEPATH = "accountName.path\\/filename" 
    builtins.py=PyImport('builtins')
    site.py=PyImport('site')
    sitepackages = PyCallMethod(site.py,'getsitepackages')
    sitepackages = PyCallMethod(builtins.py,'str',sitepackages)
    convert ",[]'" to @am in sitepackages 
    loop
        remove sitepack from sitepackages setting eos 
        if upcase(sitepack[13]) = 'SITE-PACKAGES' then 
           sitepack = trim(change(sitepack,'\\\\','\\'))
           openpath sitepack to f.sitepack then 
                  write MYMODULEPATH on f.sitepack,'myapp.pth' 
                  exit 
           end
    while eos do repeat 
    if sitepack = '' then print 'error: u2python site-packages not found'   

     

    I use .../site-packges because on Linux, site does not search sys.prefix but (sys.prefix + "/lib/site-packages").

    Do you have another idea to deploy easily ?  

    many thanks



    ------------------------------
    Manu Fernandes
    ------------------------------

    Hi Manu,

    Your example is indeed a good way to make your modules available to UV's python.

    An alternative, which follows a more traditional installation workflow, is to put your module into a package, and then use pip install path/to/package. 

    Here is a video describing packaging code into a python package: https://www.youtube.com/watch?v=GaWs-LenLYE

    In summary,

    1. Create the following directory structure for your code

    mypypackage/
    ├── mypymodule
    │   ├── __init__.py
    │   └── mypycode.py
    └── setup.py

    Where

    mypycode.py (for example) holds your code:

    def mypyfunction():
            print("howdy from mypyfunction!")

    __init__.py can be empty:

      

    setup.py describes your package:

    from setuptools import find_packages
    from setuptools import setup

    setup(
            name='mypymodule',
            version='1.0.0',
            description='my py module',
            packages=find_packages(),
    )

    Then, you can execute (via BASIC EXECUTE, or however you prefer) the following:

    /usr/uv/python/bin/pip install /path/to/mypypackage/

    which will install your package into UV python's site-packages directory

    Output:

    /usr/uv/XDEMO $ /usr/uv/bin/uv

    Welcome to the XDEMO Account

    python> from mypymodule import mypycode
    python> mypycode.mypyfunction()
    howdy from mypyfunction!



    ------------------------------
    Steven Wingfield
    Principal Software Engineer
    Rocket Software
    College Station, TX, USA
    ------------------------------