IBM i Global

 View Only
Expand all | Collapse all

$PATH on a CLP?

  • 1.  $PATH on a CLP?

    Posted Mon January 23, 2023 09:41 AM
    Edited by Paulo Silva Mon January 23, 2023 10:24 AM
    Hi,

    I'm running a python scrip from a CLP with QSH with the following command: /QOpenSys/pkgs/bin/python3.9 mypythonscript.py and everything is working perfectly.
    However when I do: python3.9 mypythonscript.py I get the message: qsh: 001-0019 Error found searching for command python3.9. No such path or directory. 

    I'm using the my own user profile.
    This is what I see in the QSH shell:
    echo $PATH
    /QOpenSys/pkgs/bin:/usr/bin:.:/QOpenSys/usr/bin

    With the same user btw running call QP2TERM, I get something different:
    echo $PATH
    /QOpenSys/pkgs/bin:/QOpenSys/usr/bin:/usr/ccs/bin:/QOpenSys/usr/bin/X11:/usr/sbin:.:/usr/bin

    Am I doing anything wrong?

    Thanks in advance,
    Paulo

    ------------------------------
    Paulo Silva
    ------------------------------


  • 2.  RE: $PATH on a CLP?

    IBM Champion
    Posted Mon January 23, 2023 10:20 PM
    Edited by Satid Singkorapoom Mon January 23, 2023 10:22 PM
    Dear Paulo

    I guess you need to explicitly set your PATH environment variable and you can do this in more than one way as described here : https://ibmi-oss-docs.readthedocs.io/en/latest/troubleshooting/SETTING_PATH.html  

    There are some tips on using Python in IBM i here : https://ibmi-oss-docs.readthedocs.io/en/latest/python/README.html  The URL above is the first hot link of this Python Usage Note page.

    Scott Klement also indicates this may be done from CL.  So, please try to identify whichever suits your case.


    ------------------------------
    Right action is better than knowledge; but in order to do what is right, we must know what is right.
    -- Charlemagne

    Satid Singkorapoom
    ------------------------------



  • 3.  RE: $PATH on a CLP?

    Posted Tue January 24, 2023 07:06 AM
    Dear Satid,

    Thanks a lot for your help.

    I have my user $PATH defined correctly and I was expecting that to be enough to run QSH commands.
    From what I see that's not the case and it is still mandatory to define an environment variable on the CLP as suggested by Scott Klement.

    That's what I did.

    Thanks again Satid!

    BR,
    Paulo

    ------------------------------
    Paulo Silva
    ------------------------------



  • 4.  RE: $PATH on a CLP?

    Posted Wed January 25, 2023 11:02 AM
    I can shed a bit more light on this issue. 

    First of all, for many reasons, calling your Python script using QSH is the right thing to do.  There are other options (QP2SHELL, QP2SHELL2 and QP2TERM), but they are more specialized and generally more difficult to use.  Even though Python is a PASE program, QShell is the environment that is most likely to get all of your I/O where it belongs and properly converted to the correct ccsid, even when using a PASE program.

    The difference you get in the environment is based on the QShell handling of the .profile or /etc/profile file where you most likely define your environment variables with something like:

    PATH=$PATH:/QOpenSys/pkgs/bin

    QSH only sources this file if you are running an interactive shell.  By definition, if you specify a command, it is not an interactive shell, so no .profile file processing.

    Most Unix shells have alternative files that will run for both interactive and non-interactive shells, but QSH does not.

    The simple solution is just to force the .profile (or any file of your choice) to get sourced using the syntax:

    QSH CMD('. $HOME/.profile;python3.9 myscriptname.py')

    You may have noticed that there is an extra period at the beginning that looks out of place.  That tells the Unix shell to SOURCE the file rather than EXECUTE the file.  The difference is that sourcing a file just means running the commands in the current job level and context.  That means the change to the environment variable occurs in the current process, not a sub-process that would immediately go away taking the newly set environment variable with it.

    The $HOME gets expanded by the shell to the current HOME directory of the user, and the semicolon just separates multiple commands to be processed by the shell.

    I very much prefer this method to the ADDDENVVAR in the CL program, primarily because it allows the Unix environment to be managed by the Unix environment, and is much easier to include the default PATH without hardcoding it in a CLP.

    An alternative is:

    QSH CMD('PATH=$PATH:/QOpenSys/pkgs/bin python3.9 myscriptname.py')

    That just explicitly sets the path in the command.  It is better than the ADDENVVAR approach, but hardcodes a value that, in my opinion, is better softcoded in the environment where it will be run.

    One more thing.  In my examples, you may have noticed I put the added directory at the end of the path, rather than the beginning.  That is because there is some overlap in the QSH and PASE environment binaries.  If there are two versions of the binary commands, you typically want the QShell version rather than the PASE version when you are running in the PASE environment.

    ------------------------------
    Vincent Greene
    IT Consultant
    Technology Services
    IBM
    Vincent.Greene@ibm.com


    The postings on this site are my own and don't necessarily represent IBM's positions, strategies or opinions.
    ------------------------------



  • 5.  RE: $PATH on a CLP?

    Posted Wed January 25, 2023 01:43 PM
    Hi Vincent,

    This is very good insight and I already tested it and have it working.
    I totally agree with you, this should not be hardcoded and it should follow the environment where the script is running. I was using the ADDENVVAR method because I didn't know anything better but what you explained is exactly what I was looking for. 

    Thanks a lot

    Paulo

    ------------------------------
    Paulo Silva
    ------------------------------



  • 6.  RE: $PATH on a CLP?

    Posted Thu January 26, 2023 02:01 AM
    Let me check on the PATH settings.

    > One more thing. In my examples, you may have noticed I put the added directory at the end of the path, rather than the beginning.

    "IBM i OSS Docs" says "The PATH should contain, at minimum, /QOpenSys/pkgs/bin, very preferrably before the "standard" paths."
    Would it be best practice to have $PATH at the beginning when calling the PASE program from CL via qshell and $PATH at the end when calling from ssh?

    ------------------------------
    Hideyuki Yahagi @ Japan
    ------------------------------



  • 7.  RE: $PATH on a CLP?

    Posted Thu January 26, 2023 10:41 AM
    Interesting.  I never saw that page.  Thank you for bringing that to my attention.

    If you are executing the command via ssh, you are entirely within the PASE environment, so having the PASE commands at the front of the PATH would probably be preferable.  I should note that your path in that environment is determined via a different process that involves the SSHD configuration, and the specific shell that is executed, so sourcing the .profile file in your call is probably not desirable.  That means no changes are likely to be necessary for your ssh calls.

    If you are executing from QSH, I would expect that the preference would be to run the QShell version of commands, but it probably depends on whether those commands are going to be executed as subprocess from the PASE environment (e.g. a system command inside the Python code), or as a pipeline from the QShell environment (e.g. piping the Python output via grep).

    Bottom line is I am less confident in that part of my original answer now.  I will try to engage the IBM OSS team to find a more definitive answer and report back.  Until then, if you get ccsid conversion type issues/unreadable data, etc.  try switching the PATH to see if that resolves it. 

    If you don't have any conversion problems, there is probably no need to change the path no matter how you currently have it set.

    ------------------------------
    Vincent Greene
    IT Consultant
    Technology Services
    IBM
    Vincent.Greene@ibm.com


    The postings on this site are my own and don't necessarily represent IBM's positions, strategies or opinions.
    ------------------------------



  • 8.  RE: $PATH on a CLP?

    Posted Fri February 09, 2024 01:04 AM
    Edited by Vinit Kumar Fri February 09, 2024 01:07 AM

    First of all, a big thank you for this insightful discussion,

    We are facing the exact same situation with CCSID conversion for Arabic text string but through PASE. Through PASE environment on IBM i, we are calling py3.9 code, with Arabic text string as argument. The code creates a pdf file with qrcode and Arabic text inserted in one of the fields in pdf file. The file is exported from navigator to windows PC. The file shows Arabic text as gibberish (little boxes). However, the same stuff and code works completely fine through ssh session to IBMi. From both ways, the CCSID of the generated PDF file is 819 only. We tried to pass the Arabic text string in utf-8 format with CCSID 1208, but still the same issue. 

    After going through the post, we will try to provide the PATH variable explicitly, while calling the py3.9 from PASE environment. Hope this helps and resolves our issue. 

    Regards,

    Vinit Kumar



    ------------------------------
    Vinit Kumar
    ------------------------------



  • 9.  RE: $PATH on a CLP?

    Posted Tue February 13, 2024 10:15 AM

    It seems like you are talking about argument issue, not path.

    I think you can specify only ASCII characters when call pase shell from CL program.

    In the Note at "How to Call PASE Commands/Scripts from a Command Line, CL Program, or in a Submitted Job",
    IBM states that DBCS characters cannot be used, but may also apply to Arabic.

    There are several ways to invoke python from CL.
    If you want to path non-ASCII argument to python, I would use CGI or other method (use database, parameter file, etc.)

    Yahagi@Japan



    ------------------------------
    英幸 矢作
    ------------------------------



  • 10.  RE: $PATH on a CLP?

    Posted Thu February 15, 2024 03:47 AM

    Thanks Yahagi. So, it seems we have to find another way to pass the Arabic text string as argument. We are testing using python data base package to fetch the Arabic text string argument. 



    ------------------------------
    Vinit Kumar
    ------------------------------



  • 11.  RE: $PATH on a CLP?

    Posted Mon February 12, 2024 11:01 AM
    Edited by ace ace Mon February 12, 2024 11:03 AM

    Personally I would "always" have in .profile

    something like 

    export PATH=/QOpenSys/pkgs/bin:$PATH

    using the system via ssh I've noticed same commands in different locations, so at least it gives priority to the open package system versions.

    i.e. if one notices there are different "ls" in various incarnations

    -bash-5.2$ whereis ls
    ls: /usr/bin/ls /QOpenSys/opt/freeware/bin/ls /QOpenSys/pkgs/bin/ls /QOpenSys/usr/bin/ls
    -bash-5.2$ ls -l /usr/bin/ls
    lrwxrwxrwx 1 qsys 0 54 Dec 16  2020 /usr/bin/ls -> /QSYS.LIB/QSHELL.LIB/LS.PGM
    -bash-5.2$ ls -l /QOpenSys/usr/bin/ls
    lrwxrwxrwx 1 qsys 0 74 Dec 16  2020 /QOpenSys/usr/bin/ls -> ../../QIBM/ProdData/OS400/PASE/bin/ls
    -bash-5.2$ ls -l /QOpenSys/pkgs/bin/ls
    -rwxr-xr-x 1 qsys 0 1973395 Jul 22  2022 /QOpenSys/pkgs/bin/ls

    For automated production scripts interoperation, regarding parameter passing from an ILE to a custom PASE script (php, python...) I personally find much easier to use named environment variables to pass infos instead of pure command line parameters.

    No need to concatenate, space/quote mess, easier to read, they got definite CCSID, you can set it in CL easily, it is named so easy to understand, and you can retrieve it easily in any open language easily with no need to parse etc.etc.



    ------------------------------
    --ft
    ------------------------------