- Posts: 52
- Thank you received: 2
Hal and python3.
6 years 11 months ago - 6 years 11 months ago #150897
by dab77
Replied by dab77 on topic Hal and python3.
Unfortunately it doesn't work from python (or at least I can't..)
if doing:
i got:
don't know why, since in the terminal 'source' is well accepted..
A workaround I used in the meantime is this:
and then every hal command I need I put 'rip_cmd' before as:
if doing:
Code:
rip_cmd = ('source /home/dab/linuxcnc/scripts/rip-environment')
print(subprocess.call(rip_cmd, shell=True))
import hal
Code:
/bin/sh: 1: source: not found
...
A workaround I used in the meantime is this:
Code:
sys.path.append('/home/dab/linuxcnc/lib/python/')
rip_cmd = '/home/dab/linuxcnc/scripts/rip-environment'
import hal
Code:
halrun_U = rip_cmd + " halrun -U"
os.system(halrun_U)
..or..
launch_halcmd = rip_cmd + ' halcmd -v -f halga.hal'
os.system(launch_halcmd)
Last edit: 6 years 11 months ago by dab77.
Please Log in or Create an account to join the conversation.
6 years 11 months ago #150919
by dgarrett
The 'source' (equivalently '.') command is a shell
(/bin/bash) *builtin*. You need to 'source' the
rip-environment script in the terminal *BEFORE*
invoking your python script in order to populate the
environmental variables needed to find LinuxCNC
executables and the modules or libraries used for its
scripting languages (python,tcl). The environmental
variables created by sourcing the rip-environment
script will then be available to child processes (eg
python scripts) of the terminal process.
Example (using /home/git/linuxcnc-dev as the root directory for a
LinuxCNC RIP build):
Ref:
For modern Linux distributions, the default shell used
in a terminal is bash (/bin/bash)
Replied by dgarrett on topic Hal and python3.
Unfortunately it doesn't work from python
The 'source' (equivalently '.') command is a shell
(/bin/bash) *builtin*. You need to 'source' the
rip-environment script in the terminal *BEFORE*
invoking your python script in order to populate the
environmental variables needed to find LinuxCNC
executables and the modules or libraries used for its
scripting languages (python,tcl). The environmental
variables created by sourcing the rip-environment
script will then be available to child processes (eg
python scripts) of the terminal process.
Example (using /home/git/linuxcnc-dev as the root directory for a
LinuxCNC RIP build):
Code:
$ source /home/git/linuxcnc-dev/scripts/rip-environemnt
$ python
...
>>> import os
>>> os.environ.get('PYTHONPATH')
'/home/git/linuxcnc-dev/lib/python'
Ref:
For modern Linux distributions, the default shell used
in a terminal is bash (/bin/bash)
Code:
$ man bash
...
SHELL BUILTIN COMMANDS
...
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current shell en‐
vironment and ... (much more)
Please Log in or Create an account to join the conversation.
6 years 11 months ago #150925
by dab77
Replied by dab77 on topic Hal and python3.
Ok, I should have said I'm developing in pycharm, and sourcing the rip-environment in a terminal doesn't help if then I run my code from inside pycharm.
Please Log in or Create an account to join the conversation.
6 years 11 months ago #150929
by dgarrett
I don't know anything about pycharm but find it suprising
that it doesn't propagate a shell environment. Perhaps
you start pycham from a menu -- if so, you could try
starting pycharm in a terminal *after* sourcing rip-environemnt.
If that guess is unhelpful, you would need to set all the
the paths required by LinuxCNC within your python program.
You can ascertain which items are set or updated by
rip-environment by comparing the environment before and
after sourcing rip-environment. Example:
Replied by dgarrett on topic Hal and python3.
I run my code from inside pycharm.
I don't know anything about pycharm but find it suprising
that it doesn't propagate a shell environment. Perhaps
you start pycham from a menu -- if so, you could try
starting pycharm in a terminal *after* sourcing rip-environemnt.
If that guess is unhelpful, you would need to set all the
the paths required by LinuxCNC within your python program.
You can ascertain which items are set or updated by
rip-environment by comparing the environment before and
after sourcing rip-environment. Example:
Code:
$ # new shell before sourcing rip-environment
$ env >| before.txt
$ source /home/git/linuxcnc-dev/scripts/rip-environment
$ env >| after.txt
$ diff before.txt after.txt
Please Log in or Create an account to join the conversation.
Time to create page: 0.046 seconds