1
$\begingroup$

I am working on a docker and using ROS 1 noetic as of now.

For certain reasons I do not want to distribute .py files as end user do not need to understand what is inside there. So I am using .pyc files generated using python module compileall.

When I try to launch .pyc file on the local machine with Ubuntu (not on the docker) it works perfectly fine. But when I put it inside the docker, it throws the following error while launching.

If it is a script, you may be missing a '#!' declaration at the top.

I have already provided execution permition to the file. Any idea what am I missing here?

[Update]

Progress.. A different error.. After updating CMakeLists.txt by adding following,

catkin_install_python(PROGRAMS 
  scripts/control_lib.pyc
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

Now I am getting RuntimeError: Bad magic number in .pyc file

But it doesn't make sense as file is compilled on the docker.

[Try it yourself]

Here is a simple example to reproduce the error. https://github.com/aarsht7/pyc_test

It contains simple talker node with python.

When used .py file in the roslaunch, it behaves as it should be. While usin .pyc file in roslaunch, it throws RuntimeError: Bad magic number in .pyc file

$\endgroup$
7
  • $\begingroup$ I checked python version on both, they are similar. I tried to complie the file on the docker and use that file instead of cloning pyc generated on local machine, did not worked. same error in both the cases $\endgroup$
    – aarsht7
    Commented Dec 14, 2023 at 16:32
  • $\begingroup$ This seems to be a generic python problem and only related to robotics by where you want to use it. $\endgroup$
    – Tully
    Commented Dec 15, 2023 at 0:20
  • $\begingroup$ I'll convert this to an answer if this gets reopened, this worked (outside of docker at least, haven't tested inside it) using python 3.11: python -m compileall `rospack find talker`, <node pkg="talker" type="talker.sh" name="talker_pyc" output="screen"/> then in scripts/talker.sh wrapper script: #!/bin/bash python `rospack find talker`/scripts/__pycache__/talker.cpython-311.pyc $\endgroup$ Commented Dec 18, 2023 at 17:19
  • $\begingroup$ Thing is pyc files worked on the local machine as well. it's just docker that is struggling with pyc. I will try your suggestion in docker for confirmation. But for now, I have decided to try pyinstaller to generate distributable bundle file. Just that it take up more storage than the pyc file as it packs all the dependancy togather so I am not sure if I will continue using pyinstaller $\endgroup$
    – aarsht7
    Commented Dec 19, 2023 at 8:48
  • $\begingroup$ I tried docker and see the same RLException: Unable to launch If it is a script,.. (I think any time you saw Bad magic that's from running a pyc from a different python version, probably the outside one)- but the wrapper worked. So the issue is something different with any combination of ros or python versions or the docker environment vs. outside docker. $\endgroup$ Commented Dec 20, 2023 at 15:38

0

Browse other questions tagged or ask your own question.