Skip to main content
Dec 7, 2020 11:03 AM UTC

U2PE, Python and virtual environments

Hello Everyone,  I have installed Rocket U2/Universe Personal Edition (12.1.1/Python 3.7.3) and I'm trying to get it to work a Python virtual environment and not being successful.  

  1. Is it possible to use virtual environments with Universe Python and if so,
  2. Are there any (or can one provide) instructions for doing so?

Thanks,

Dave Howell

------------------------------
David Howell
Market America
------------------------------

    12 replies

    Mike Rajkowski
    Rocketeer
    Feb 22, 2021 6:55 PM UTC

    Yes, an alias is an option.
    They certainly can automate the steps needed to resolve the correct path to the python binary.
    The downside is that it has to be applied during the user session to establish it in the first place.

    We setup our user logins to have the UV bin dir in the user's path, so having a uvpython command within the UV bin directory makes sense for us.

    The current script I have for uvpython is below, and caters for the custom_env setup as well as properly setting the LD_LIBRARY_PATH environment variable.
    The end result is that python run form uvpython will have the same operating environment as python launched from within the UV shell.
    It has been written for Bash on RHEL servers.

    (This forum interface does not appear to offer better options for presenting code  - the PREFORMATTED paragraph setting looks like it just removes line feeds so I did not apply it here. Instead I hand modified the HTML with <pre></pre> tags)

    ----------------

    #!/bin/bash
    ##############################################################################
    # uvpython - Script to locate the current uv-based python installation and
    # forward all command line arguments to the uv python3 binary
    #
    # uvpython - Version 1.0.5 Date 20-August-2020
    #
    # (c) Copyright Pentana Solutions 2019,2020. All Rights Reserved.
    ##############################################################################

    # Finds the appropriate path to the uv-integrated python
    # then execs the python3 command, passing existing command line params

    # get our UV installation directory
    if [ ! -f /.uvhome ];then
    echo "Missing /.uvhome - cannot find python installation."
    exit 1
    fi
    UV_instdir=$(cat /.uvhome)
    # Check we have a value, and it is a path
    if [ -z "${UV_instdir}" ];then
    echo "Empty /.uvhome - cannot find python installation."
    exit 1
    elif [ ! -d "${UV_instdir}" ];then
    echo "${UV_instdir} is not found - cannot find python installation."
    exit 1
    fi

    # get the configured python path
    if [ ! -f ${UV_instdir}/.pyconfig ];then
    echo "Missing ${UV_instdir}/.pyconfig - cannot find python installation"
    exit 1
    fi
    # now source this config file to get variables set
    . ${UV_instdir}/.pyconfig

    # This *SHOULD* have populated 2 environment variables:
    # PYHOME & PYLIB

    # Check we have an environment variable, and it is a path
    if [ -z "${PYHOME}" ]; then
    echo "Missing PYHOME from ${UV_instdir}/.pyconfig - cannot find python installation"
    exit 1
    elif [ ! -d "${PYHOME}" ];then
    echo "Directory ${PYHOME} is not found - cannot find python installation."
    exit 1
    fi

    # build our python binary path
    # hard-code python3, for now
    UVPYBIN=${PYHOME}/bin/python3
    if [ ! -f "${UVPYBIN}" ]; then
    echo "Missing python3 from ${PYHOME}/bin - cannot find python installation"
    exit 1
    elif [ ! -x "${UVPYBIN}" ];then
    echo "${UVPYBIN} is not executable - cannot find python installation"
    exit 1
    fi

    # Ensure we have an appropriate LD_LIBRARY_PATH for uv+python
    # Logic sourced from /usr/uv/bin/uv bash script
    if [ -z "${LD_LIBRARY_PATH}" ];then
    # Likely that we are NOT being launched from within a bash shell
    # launched by the "SH" TCL command - most likely that we are root
    # Start with the bin directory in our UV install directory
    # and the lib path in the uv-configured python directory
    LD_LIBRARY_PATH="${UV_instdir}/bin:${PYHOME}/lib"
    # on linux we also prefix it with /usr/lib64
    if [[ ! ${LD_LIBRARY_PATH} == *"/usr/lib64"* ]]; then
    LD_LIBRARY_PATH=/usr/lib64:${LD_LIBRARY_PATH}
    fi
    fi

    # Check for custom variables (ie. PS1 redefined)
    if [ -f "${UV_instdir}/bin/custom_env" ];then
    # Source the customized variables (should not be a shell script)
    . ${UV_instdir}/bin/custom_env
    fi
    # Make sure we have LIBRARY_PATH assigned
    # LIBRARY_PATH is used when python installs modules and needs to compile+link C code
    if [ -z "${LIBRARY_PATH}" ];then
    LIBRARY_PATH=${LD_LIBRARY_PATH}
    fi

    # now run the python binary, passing all our command line arguments, and
    # setup the LD_LIBRARY_PATH and LIBRARY_PATH env variables for the exec'd binary
    exec env LD_LIBRARY_PATH=${LD_LIBRARY_PATH} LIBRARY_PATH=${LIBRARY_PATH} ${UVPYBIN} "$@"
    # we don't come back from this.

    ----------------



    ------------------------------
    Gregor Scott
    Software Architect
    Pentana Solutions
    Mount Waverley VIC Australia
    ------------------------------
    Gregor,

    I do like the script you wrote, and I am glad it works for you. 

    I will need to discuss this and other options with engineering since there are pros and cons to each solution, and there may not be one that works for everyone.

    As for the issue with the code the forum, it looked fine to me.


    ------------------------------
    Michael Rajkowski
    Rocket Software
    ------------------------------
    David Andrews
    Rocketeer
    Feb 23, 2021 7:53 AM UTC

    Yes, an alias is an option.
    They certainly can automate the steps needed to resolve the correct path to the python binary.
    The downside is that it has to be applied during the user session to establish it in the first place.

    We setup our user logins to have the UV bin dir in the user's path, so having a uvpython command within the UV bin directory makes sense for us.

    The current script I have for uvpython is below, and caters for the custom_env setup as well as properly setting the LD_LIBRARY_PATH environment variable.
    The end result is that python run form uvpython will have the same operating environment as python launched from within the UV shell.
    It has been written for Bash on RHEL servers.

    (This forum interface does not appear to offer better options for presenting code  - the PREFORMATTED paragraph setting looks like it just removes line feeds so I did not apply it here. Instead I hand modified the HTML with <pre></pre> tags)

    ----------------

    #!/bin/bash
    ##############################################################################
    # uvpython - Script to locate the current uv-based python installation and
    # forward all command line arguments to the uv python3 binary
    #
    # uvpython - Version 1.0.5 Date 20-August-2020
    #
    # (c) Copyright Pentana Solutions 2019,2020. All Rights Reserved.
    ##############################################################################

    # Finds the appropriate path to the uv-integrated python
    # then execs the python3 command, passing existing command line params

    # get our UV installation directory
    if [ ! -f /.uvhome ];then
    echo "Missing /.uvhome - cannot find python installation."
    exit 1
    fi
    UV_instdir=$(cat /.uvhome)
    # Check we have a value, and it is a path
    if [ -z "${UV_instdir}" ];then
    echo "Empty /.uvhome - cannot find python installation."
    exit 1
    elif [ ! -d "${UV_instdir}" ];then
    echo "${UV_instdir} is not found - cannot find python installation."
    exit 1
    fi

    # get the configured python path
    if [ ! -f ${UV_instdir}/.pyconfig ];then
    echo "Missing ${UV_instdir}/.pyconfig - cannot find python installation"
    exit 1
    fi
    # now source this config file to get variables set
    . ${UV_instdir}/.pyconfig

    # This *SHOULD* have populated 2 environment variables:
    # PYHOME & PYLIB

    # Check we have an environment variable, and it is a path
    if [ -z "${PYHOME}" ]; then
    echo "Missing PYHOME from ${UV_instdir}/.pyconfig - cannot find python installation"
    exit 1
    elif [ ! -d "${PYHOME}" ];then
    echo "Directory ${PYHOME} is not found - cannot find python installation."
    exit 1
    fi

    # build our python binary path
    # hard-code python3, for now
    UVPYBIN=${PYHOME}/bin/python3
    if [ ! -f "${UVPYBIN}" ]; then
    echo "Missing python3 from ${PYHOME}/bin - cannot find python installation"
    exit 1
    elif [ ! -x "${UVPYBIN}" ];then
    echo "${UVPYBIN} is not executable - cannot find python installation"
    exit 1
    fi

    # Ensure we have an appropriate LD_LIBRARY_PATH for uv+python
    # Logic sourced from /usr/uv/bin/uv bash script
    if [ -z "${LD_LIBRARY_PATH}" ];then
    # Likely that we are NOT being launched from within a bash shell
    # launched by the "SH" TCL command - most likely that we are root
    # Start with the bin directory in our UV install directory
    # and the lib path in the uv-configured python directory
    LD_LIBRARY_PATH="${UV_instdir}/bin:${PYHOME}/lib"
    # on linux we also prefix it with /usr/lib64
    if [[ ! ${LD_LIBRARY_PATH} == *"/usr/lib64"* ]]; then
    LD_LIBRARY_PATH=/usr/lib64:${LD_LIBRARY_PATH}
    fi
    fi

    # Check for custom variables (ie. PS1 redefined)
    if [ -f "${UV_instdir}/bin/custom_env" ];then
    # Source the customized variables (should not be a shell script)
    . ${UV_instdir}/bin/custom_env
    fi
    # Make sure we have LIBRARY_PATH assigned
    # LIBRARY_PATH is used when python installs modules and needs to compile+link C code
    if [ -z "${LIBRARY_PATH}" ];then
    LIBRARY_PATH=${LD_LIBRARY_PATH}
    fi

    # now run the python binary, passing all our command line arguments, and
    # setup the LD_LIBRARY_PATH and LIBRARY_PATH env variables for the exec'd binary
    exec env LD_LIBRARY_PATH=${LD_LIBRARY_PATH} LIBRARY_PATH=${LIBRARY_PATH} ${UVPYBIN} "$@"
    # we don't come back from this.

    ----------------



    ------------------------------
    Gregor Scott
    Software Architect
    Pentana Solutions
    Mount Waverley VIC Australia
    ------------------------------
    Hi Forum members. I wanted to point out we do have a way to share code on the site.  It doesn't appear this is the universal symbol for code (;-) so some people have missed it. Give it a try when you have the need! Dave



    ------------------------------
    Dave Andrews
    Head of Customer Engagement
    Rocket Software
    ------------------------------