pythonenv

Package: WA2L/edrc 1.5.57
Section: Library Commands (3)
Updated: 25 March 2025
Index Return to Main Contents

 

NAME

pythonenv - print environment needed to start Python3 scripts

 

SYNOPSIS

edrc/lib/pythonenv [ -n ]

 

AVAILABILITY

WA2L/edrc

 

DESCRIPTION

print the environment used by pythonenv to access the Python3 packages/modules bundled with WA2L/edrc.

To set the environment prior to the execution of python3, invoke:

  eval `pythonenv` 

This command is *only* to be used to prepare the environment to execute Python3 scripts in "Recovery Script" trees.

When writing Python3 scripts for WA2L/edrc, do *not* use pythonenv, start the script thru the .python_wrapper. See python_wrapper(1) for information about integrating a Python3 script into WA2L/edrc.

 

OPTIONS

-n
no export VARIABLE ... output.

 

ENVIRONMENT

$PYTHONPATH
this variable holds Python3 package/module locations additional to the standard applying to an installation. This variable will be expanded with the bundled Python3 module locations (in the Python3 virtual environment venv) when executing the pythonenv command.

$VIRTUAL_ENV
path of the Python3 virtual environment (venv) for the related operating system id and Python3 version (.../$OSID/<major>.<minor>/) bundled with WA2L/edrc.

$PATH
command search path. This variable will be expanded with the path where the python3 interpreter was found on the system when executing the pythonenv command. See also python_wrapper(1) for more information.

 

EXIT STATUS

0
always

 

FILES

edrc/etc/python_wrapper.cfg
configuration file for .python_wrapper and pythonenv.

edrc/lib/python/pym/
location of all bundled Python3 packages/modules. The related directory in edrc/lib/python/pym/<OSID>/<major>.<minor>/lib/python<major>.<minor>/site-packages/ is pre-pended to the $PYTHONPATH environment variable when executing pythonenv.

Where <major> is the major version number (e.g. 3) and <minor> is the minor Python3 version (e.g. 6).

 

EXAMPLES

1) Start a Python3 script from a recovery script

To start a Python3 script that uses a bundled Python3 module from a recovery script, set the Python3 environment using the 'eval `pythonenv`' command in the recovery script and then call the python3 interpreter.

Recovery script:

  #!/bin/ksh
  #
  # 1:ascript - A Recovery Script  
  #
  # [00] 08.02.2009 CWa Initial Version
  #
  #
  test "$DEBUG" = True && set -x
  :
  :
  eval `pythonenv`
  python3 ./pythonscript
  :

Python3 script:

  #
  # pythonscript - Python3 script using bundled complex module
  #
  # [00] 18.03.2009 CWa Initial Version
  #
  :
  :
  from complex import Complex
  :

2) Start a Python3 script from a recovery script using the _env file mechanism

Create an _env file (using the env edrc command) and start the Python3 script from the recovery script.

_env file:

  #
  # _env - Environment settings for commands in /apps/eg
  #
  # [00] 18.03.2009 CWa Initial Version
  #
  #
  test "$DEBUG" = True && set -x
  
  eval `pythonenv`
  :

Recovery script:

  #!/bin/ksh
  #
  # 1:ascript - A Recovery Script  
  #
  # [00] 08.02.2009 CWa Initial Version
  #
  #
  test "$DEBUG" = True && set -x
  :
  :
  python3 ./pythonscript
  :

Python3 script:

  #
  # pythonscript - Python3 script using bundled complex module
  #
  # [00] 18.03.2009 CWa Initial Version
  #
  :
  :
  from complex import Complex
  :

3) Write a recovery script in Python3

Create an _env file (using the env edrc command) and write the recovery script in Python3.

_env file:

  #
  # _env - Environment settings for commands in /apps/eg
  #
  # [00] 18.03.2009 CWa Initial Version
  #
  #
  test "$DEBUG" = True && set -x
  
  eval `pythonenv`
  :

Python3 recovery script:

  #!/usr/bin/env python3
  #
  # 1:ascript - A Recovery Script  
  #
  # [00] 08.02.2009 CWa Initial Version
  #
  :
  :
  from complex import Complex
  :

Note the use of '#!/usr/bin/env python3' in the magic key to start the Python3 script. This ensures that the python3 interpreter is found on the system, even when it is not installed in the default location you will normally specify. The path to the python3 interpreter is added to the $PATH using the pythonenv command.

4) Write a Python3 script outside of WA2L/edrc and profit from bundled Python3 modules

The startup method method below allows to start the python3 interpreter from wherever it is installed and also enables to use all python3 packages/modules bundled with WA2L/edrc using the pythonenv command to dynamically initialize the environment.

This method is similar to the use of the .python_wrapper, but the Python3 script can be placed somewhere of your liking outside of WA2L/edrc.

Set the permissions of the script to executable:

  chmod +x pythonscript

Python3 script:

  #!/bin/ksh
  #
  # pythonscript - Python3 script using bundled modules
  #
  # [00] 18.03.2009 CWa Initial Version
  #
  
  eval `~edrc/lib/pythonenv`
  
  sed '1,/^exit \$\?/d' $0 | python3 - "$0" "$@"
  exit $?
  
  
  # python
  #
  import sys
  from complex import Complex
  
  sys.argv[0] = sys.argv.pop(1); __file__ = sys.argv[0]
  
  
  def main():
      print("Hello World!\n"); 
  
      print("Arguments:", sys.argv[0:])
  
      x = Complex(2,3)
      y = Complex(4,5)
      print("x+y = {}".format(x+y))
  
      return(0)
  
  if __name__ == "__main__": sys.exit(main())

5) Test the availability of a specific Python3 package/module

Set the needed python3 environment:

  [ / ] 
  [ root@acme007 ][*edrc*/bash]: eval `pythonenv`

Try to access the python3 module:

  [ / ] 
  [ root@acme007 ][*edrc*/bash]: python -c "import complex"

If an error message like

  Traceback (most recent call last):
    File "<string>", line 1, in <module>
  ModuleNotFoundError: No module named 'complex'

appears, the package/module cannot be found, if the package/module can be found there is no output.

6) List the available Python3 packages/modules

To list the installed Python3 packages, use the pip3(1) command.

To list the packages bundled in WA2L/edrc:

  [ /opt/edrc/bin ]
  [ root@acme007 ][*edrc*/bash]: pip list --local 

To list all packages available (this includes the packages/modules bundled in WA2L/edrc):

  [ /opt/edrc/bin ]
  [ root@acme007 ][*edrc*/bash]: pip list

7) Show detail information of an installed package

To list information about installed packages, the pip3(1) command is used.

The pip show [ --file ] packagename also displays the Location where the package is installed/read.

To show detail information of a package:

  [ /opt/edrc/bin ]
  [ root@acme007 ][*edrc*/bash]: pip show complex

To show detail information and all installed files of a package:

  [ /opt/edrc/bin ]
  [ root@acme007 ][*edrc*/bash]: pip show --file complex 

 

SEE ALSO

edrcintro(1), osid(3), pkgdir(1m), python3(1), pythonversion(3), pip(1), pip3(1), python_wrapper(1), https://pypi.org/

 

NOTES

-

 

BUGS

-

 

AUTHOR

pythonev was developed by Christian Walther. Send suggestions and bug reports to wa2l@users.sourceforge.net . 

 

COPYRIGHT

Copyright © 2025 Christian Walther

This is free software; see edrc/doc/COPYING for copying conditions. There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


 

Index

NAME
SYNOPSIS
AVAILABILITY
DESCRIPTION
OPTIONS
ENVIRONMENT
EXIT STATUS
FILES
EXAMPLES
SEE ALSO
NOTES
BUGS
AUTHOR
COPYRIGHT

This document was created by man2html using the manual pages.
Time: 19:26:17 GMT, April 08, 2025