View previous topic :: View next topic |
Author |
Message |
Vieri l33t
Joined: 18 Dec 2005 Posts: 897
|
Posted: Wed Oct 16, 2024 5:20 pm Post subject: [SOLVED] Perl script: run Python env activate script |
|
|
Hi,
I'd like a Perl script to properly run a program within a Python virtual environment and retrieve its stdout text.
Something like:
Code: | `source /opt/custom_pyvenvs/program/bin/activate param1 param2' |
If I try to run this in Perl I get a "source: No such file or directory" error.
I know source is a BASH built-in command, so can I run the "activate" script directly from the Perl script within a bash shell even though the activate script is supposed to be "sourced"?
Regards,
Vieri
Last edited by Vieri on Thu Oct 17, 2024 12:21 am; edited 1 time in total |
|
Back to top |
|
|
bstaletic Guru
Joined: 05 Apr 2014 Posts: 344
|
Posted: Wed Oct 16, 2024 5:31 pm Post subject: |
|
|
Sourcing a bash script in a perl interpreter makes no sense.
On the other hand, replicating what the activate script does is almost trivial:
- Define $VIRTUAL_ENV to be the root directory of the virtual environment
- Prepend $VIRTUAL_ENV/bin to $PATH
- Mess with your $PS1
- Define deactivate function to undo everything.
I'm sure perl has its way of altering the environment. |
|
Back to top |
|
|
Vieri l33t
Joined: 18 Dec 2005 Posts: 897
|
Posted: Thu Oct 17, 2024 12:19 am Post subject: |
|
|
Thanks for the feedback.
I do not wish to reimplement what the activate script does.
In any case, the "real" issue at hand is how to properly call a bash script from Perl. I don't see why I can't source a script with the built-in source command in BASH.
In PHP-cli I can do this:
Code: | exec("source /myenv/bin/activate params", $out, $ret); |
So in Perl I thought the backticks would do the same. I read somewhere that Perl's backticks would run /bin/sh which in my case points to bash.
So then I tried the following:
Code: | $out = `bash -c "source /myenv/bin/activate params"`; |
It worked.
Not sure why, but it worked.
Thanks
[EDIT]
In other words, this works:
Code: | $out = `bash -c "source /myenv/bin/activate params"`; |
whereas this does not:
Code: | $out = `source /myenv/bin/activate params`; |
|
|
Back to top |
|
|
Hu Administrator
Joined: 06 Mar 2007 Posts: 22514
|
Posted: Thu Oct 17, 2024 12:50 am Post subject: |
|
|
Vieri wrote: | Code: | exec("source /myenv/bin/activate params", $out, $ret); |
| That actually works for your use case, or are you just showing the PHP code equivalent to what your Perl code is trying (and failing) to do? Perhaps PHP always runs a shell, even when it doesn't need one. Vieri wrote: | Code: | $out = `bash -c "source /myenv/bin/activate params"`; | It worked.
Not sure why, but it worked. | This runs bash, and gives it a shell directive on the command line to run. Contrast that to: Vieri wrote: | [EDIT] Code: | $out = `source /myenv/bin/activate params`; |
| This searches $PATH and runs a program named source, if such a program exists. However, source is not a program; it is a shell builtin. Per perldoc perlop, backticks use a shell if required. For simple cases, no shell is required, so none is used, and shell builtins are therefore not recognized. |
|
Back to top |
|
|
Vieri l33t
Joined: 18 Dec 2005 Posts: 897
|
Posted: Thu Oct 17, 2024 9:07 am Post subject: |
|
|
Thanks for the explanation!
For completeness, the PHP code I posted "works for me".
So I guess it's as you say: PHP always runs a shell.
I missed the part in Perl about "backticks use a shell if required". How does it decide if it's required or not, I still don't know.
I'll look into it asap.
Thanks again. |
|
Back to top |
|
|
|