ros2run python script without extension .py fails on windows + pixi

Open
#1,301 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

  • #1345 by @dlanov — closed without merging

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
68/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python

Research direction

Start in ros2run/ros2run/api/init.py at run_executable, then reproduce ros2 run rqt_bag rqt_bag inside a Windows pixi shell. Trace how the executable path is passed to subprocess.Popen and verify that the command works for Python scripts without a .py extension while preserving normal executable behavior.

Written by the indexing model from the issue text.

Description

bug more-information-needed
Generated by Generative AI

No response

Operating System:

Microsoft Windows [Version 10.0.26200.8875]

ROS version or commit hash:

rolling

RMW implementation (if applicable):

No response

RMW Configuration (if applicable):

No response

Client library (if applicable):

No response

'ros2 doctor --report' output
ros2 doctor --report
<COPY OUTPUT HERE>
Steps to reproduce issue
  1. pixi shell
  2. ros2 run rqt_bag rqt_bag
Expected behavior

working

Actual behavior

Traceback (most recent call last):
File "\?<path_to>\ros_ws.pixi\envs\default\Library\bin\ros2-script.py", line 33, in
sys.exit(load_entry_point('ros2cli==0.41.1', 'console_scripts', 'ros2')())
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
File "<path_to>\ros_ws.pixi\envs\default\Lib\site-packages\ros2cli\cli.py", line 91, in main
rc = extension.main(parser=parser, args=args)
File "<path_to>\ros_ws.pixi\envs\default\Lib\site-packages\ros2run\command\run.py", line 110, in main
return run_executable(path=path, argv=args.argv, prefix=prefix)
File "<path_to>\ros_ws.pixi\envs\default\Lib\site-packages\ros2run\api_init_.py", line 64, in run_executable
process = subprocess.Popen(cmd)
File "<path_to>\ros_ws.pixi\envs\default\Lib\subprocess.py", line 1039, in init
self._execute_child(args, executable, preexec_fn, close_fds,
~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pass_fds, cwd, env,
^^^^^^^^^^^^^^^^^^^
...<5 lines>...
gid, gids, uid, umask,
^^^^^^^^^^^^^^^^^^^^^^
start_new_session, process_group)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<path_to>\ros_ws.pixi\envs\default\Lib\subprocess.py", line 1553, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
# no special security
^^^^^^^^^^^^^^^^^^^^^
...<4 lines>...
cwd,
^^^^
startupinfo)
^^^^^^^^^^^^
OSError: [WinError 193] %1 は有効な Win32 アプリケーションではありません。

Additional information

Many executables of ros2 , such as rqt_bag, are python codes without ".py" extension, but Windows doesn't recognize shebang without py.exe, and pixi doesn't use py.exe.

I'm not sure whether there are any mechanisms of generating execution wrappers for them in case of Windows, or they weren't work for some reasons.

My suggestion is to add checking a shebang of an executable on running. I'm afraid I've not covered all the possible shebangs for Python.
https://github.com/ros2/ros2cli/blob/4eaefee77c3a66c2f724f56096539f009bb576a8/ros2run/ros2run/api/__init__.py#L58

def _check_shebang_python(path):
    import re
    shebang_python_cmds=["/usr/bin/env python3", "/usr/bin/python3", "/usr/bin/env python", "/usr/bin/python"]
    with open(path,"r") as fp:
        if fp.read(2) == '#!': #shebang
            shebang_cmd=re.sub("[\r\n]+$","",fp.readline())
            if shebang_cmd in shebang_python_cmds:
                return True
    return False

def run_executable(*, path, argv, prefix=None):
    cmd = [path] + argv

    if os.name == 'nt' and ( path.endswith('.py') or _check_shebang_python(path) ):
            cmd.insert(0, sys.executable)
Dominant language
Python
Stars
274
Forks
246
Avg merge
1d 14h
Merged PRs (30d)
12

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ros2/ros2cli

All issues in ros2/ros2cli

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.