- Is it possible to use virtual environments with Universe Python and if so,
- Are there any (or can one provide) instructions for doing so?
Thanks,
Dave Howell
------------------------------
David Howell
Market America
------------------------------
Hello Michael, Thanks for the info. I think I'll just stick with the standard UV implementation for PE as it relates to U2PY and pip install all my to-import packages there.
Regards.
Hello Michael, Thanks for the info. I think I'll just stick with the standard UV implementation for PE as it relates to U2PY and pip install all my to-import packages there.
Regards.
$ pip freezeand
$ pip install -r requirements.txt
$ pip freezeand
$ pip install -r requirements.txt
I have not explored using virtual environments with UV's python, and given how they work I think the only way you will find success is to activate the virtual environment before you launch uv so that UV inherits the active environment setup, including the PATH settings.
I think you would also need to be mindful to use the uv-supplied python3 or pip3 commands to ensure the virtual environment you setup uses the UV-supplied python installation. Either an explicit path to the binary, or having the UV python bin directory at the front of your PATH environment variable should help with this.
It is a pity that there is no uv-supplied command-line launcher for python or pip to ensure you run the correct installation. Something like uvpython or uvpip would go a long way to helping you out with this.
I have not explored using virtual environments with UV's python, and given how they work I think the only way you will find success is to activate the virtual environment before you launch uv so that UV inherits the active environment setup, including the PATH settings.
I think you would also need to be mindful to use the uv-supplied python3 or pip3 commands to ensure the virtual environment you setup uses the UV-supplied python installation. Either an explicit path to the binary, or having the UV python bin directory at the front of your PATH environment variable should help with this.
It is a pity that there is no uv-supplied command-line launcher for python or pip to ensure you run the correct installation. Something like uvpython or uvpip would go a long way to helping you out with this.
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.
----------------
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.
----------------
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.
----------------

Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.