I have use a robot simulator which only works on ROS Indigo. I also have to use Tensorflow and Jupyter Notebook so I have created a virtual environment with Python3.6.
I want to import a file from the simulator package in a notebook. So after I source bin/activate for the virtualenv, I source devel/setup.bash from my catkin workspace. But even after doing this I get the following error
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-8-8370e76446ee> in <module>
----> 1 import herbpy
~/my_workspace/devel/lib/python2.7/dist-packages/herbpy/__init__.py in <module>
33 for __execfile in __execfiles:
34 with open(__execfile, 'r') as __fh:
---> 35 exec(__fh.read())
36 del __fh
37 del __execfile
~/my_workspace/devel/lib/python2.7/dist-packages/herbpy/__init__.py in <module>
ModuleNotFoundError: No module named 'herb'
I tried running a python file importing this module (while in the environment). It only ran with python2. But with python2, I can't use Tensorflow.
How do I import this file in my notebook?
There's a few things. Remember that you can specify the version of python to run via the shebang in your script:
#!/usr/bin/env python # For "both" versions, not recommended for ros
#!/usr/bin/env python2 # For ros, to access the python/pip packages only installed for py2
#!/usr/bin/env python3 # For python3 code, to avoid that 'python' usually selects py2
You can call different python executables from launch files this way:
<?xml version="1.0"?>
<launch>
<!-- py3_ex.launch -->
<node name="call_py3" pkg="my_new_pkg" type="call_py3.py" output="screen">
</node>
</launch>
#!/usr/bin/env python3
# call_py3.py
# Try to print out the python version
import sys
import platform
#import rospy
#rospy.init_node("call_py3");
print("\n\nPython Version:\n")
print(sys.version)
print(platform.python_version())
print("\n\n")
#rospy.spin()
The above script will run python3 code (printing the version number), including tensorflow. The issue is that it, on trying to import rospy on py3 instead of py2, it won't find some of it's dependencies (such as rospkg) and give a fatal error.
To get around this, (assuming you don't switch to Ubuntu 20 + ROS Noetic, with native py3 support, or just use py2), you can call py3 scripts from your py2 ros node, such as with subprocess and piping. Or better yet, socket udp send over localhost your data/images/results/inputs, that might be the simplest way. To go this way, (and it works!) make a import socket udp connection to a localhost (127.0.0.1) random port (ex 5050), on both sides. Launch both scripts. Your py3 script can't import rospy but it can import ros msgs: from sensor_msgs.msg import Image. You can find a means to serialize it (there's multiple ways to turn a py obj into string and back).
A more painful way to setup python3 (but should be easier to use after) is to somehow replace the python2 packages to use ros with python3. There are a bunch of guides out there, and plenty of people who've ran into issues posted on SO.
Related
I am trying to make a nmap scanner for the InfoSec Certification on freeCodeCamp.org and cannot get Visual Studio Code to recognize that I have installed nmap. I am very beginner and in the process of learning.
from cProfile import run
import nmap
scanner = nmap.PortScanner()
print("Welcome, this is a simple automattion tool")
print("<------------------------------------------->")
When I run this in VS Code I get the following in the terminal:
PS C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1> python3 scanner.py
Traceback (most recent call last):
File "C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1\scanner.py", line 2, in <module>
import nmap
ModuleNotFoundError: No module named 'nmap'
PS C:\Users\mjame\OneDrive\Documents\Jim\Coding\fcc_python_for-pen_testing\nmap_scanner_1>
I have so far:
Updated to the current Python 3.10.7
Installed Nmap the first time from https://nmap.org/ for Windows
Uninstalled Nmap
Reinsalled Nmap using >>>pip3 install python-nmap
For future searchers (as in my comment above), if you installed a module with pip3 but are still getting module import errors, try python3 -m pip install <module-name>.
Not sure how/why this happens (pip3 installing somewhere that python3 is not looking for modules - maybe PYTHONPATH-related), but the above can [usually] help.
It seems that Python cannot find nmap module.
You can either add it to path in Windows via Start -> Edit system environment variables -> Environment variables... and then edit PATH VARIABLE by adding at the end path to nmap module that you have installed or move the module to default Python modules directory which should be C:\Users\YourLogin\AppData\Local\Programs\Python\Python310\Lib\site-packages
I have a question that I assume has a simple answer, but for some reason I am struggling to find it on my own. I have created and activated a virtual environment with virtualenv, and I am trying to install all the necessary packages in order to create a requirements.txt file.
I have, for example, a Python file that begins like this:
import xml.etree.ElementTree as ET
from lib.project import Projector
from lib import writer
import os
import datetime
from datetime import timedelta
from datetime import datetime
import pprint
When I try to run this file from the virtual machine, I receive the following error:
Traceback (most recent call last):
File "readMap.py", line 2, in <module>
from lib.project import Projector
ModuleNotFoundError: No module named 'lib.project'
My problem is that I'm not sure why the virtual environment can't find project.py. My directory structure is:
regiaoSul
lib
__init__.py
arrival_conversion.py
coord_conversion.py
message_conversion.py
project.py
route_conversion.py
stop_conversion.py
wkt_parser.py
writer.py
readMap.py
json_generator.py
The import on line 2 implies lib is a module rather than "a simple repository".
I will try running the script with the flag -m. Something like this -
python -m script_name
make sure to drop the .py extension when you run with -m flag.
Another advice: you don't need to install python files to the virtual environment, they are not some external libraries. They only need to be present (with the same order of packaging) when you run your script.
Thanks to everyone who responded. I believe the issue was some sort of dependency problem. In readMap.py I had imported writer from lib, and in writer.py I had imported Projector from project. I moved the function that required Projector from writer.py to readMap.py and it worked.
I still don't fully understand why this was a problem. Until recently I had been running my scripts in PyCharm and they all worked with the structure I had. It was only when I tried to run them from the command line in my virtual machine that they didn't work.
If anybody would like to explain the distinction to me and what the exact problem was with my imports, feel free to.
I sometimes face the same issue. A solution is to add the path to sys.path by:
import sys
sys.path.insert(0, "/path/to/your/package_or_module")
I’m trying to link a Raspberry pi with pycroft with a Turtlebot via SSH. I have created a skill and I want that when I said “go to somewhere” the turtlebot goes to this place.
No problem with this part. The problem come here.
I have a python script and it works fine if firstly I connect via ssh and then I execute python go_to_specific_point_on_map.py
$ ssh tb2#192.168.0.158
$ python go_to_specific_point_on_map.py
Image of everything working fine
But if I tried to do all in one command, I get:
ImportError: No module named ‘rospy’
$ ssh tb2#192.168.0.158 python go_to_specific_point_on_map.py
Image of the error. ImportError: No module named rospy
^[Traceback (most recent call last):
File "./mubita/go_to_specific_point_on_map.py", line 22, in <module>
import rospy
ImportError: No module named rospy
I have tried the arunp9294's solution but I get the same error.
$ ssh tb2#192.168.0.158 "source ~/.bashrc; python go_to_specific_point_on_map.py"
The file go_to_specific_point_on_map.py is here:
go to specific point on map script
I think it is a problem due to .bashrc is not loaded and the alias neither. I don’t know exactily what the problem is and how to solve it.
Can somebody help me? please.
I don’t know how to get it to work.
Thank you very much and best regards
I'm guessing it's a path problem, try adding the location of the rospy module to the pythonpath with:
import sys
sys.path.append("/path/to/rospy/in/your/turtlebot")
import rospy
the problem with this is that you have to manually change the path if the code is executed on different systems with different locations of the 'rospy' module
If you don't know the path you can first execute a python program like
import sys
print(sys.path)
in both ways (directly in the ssh, and after the ssh) and see if there is a difference in the output, if you see a difference just extend the path in your code with that difference.
example:
output with execution after connection:
$ ssh tb2#192.168.0.158
$ python print_path.py
['a', 'b', 'c', 'd']
output with connection and execution in one command:
$ ssh tb2#192.168.0.158 python print_path.py
['a', 'b']
if you see a difference then modify the code in the turtlebot with:
import sys
sys.path.extend(['c', 'd'])
import rospy
...
I want to run a bash script by using ProcessBuilder. This is my xtend code:
new ProcessBuilder().inheritIO().command("/bin/bash", "-c", "./myscript.sh")
This is my bash script:
#!/bin/bash
python WebRoot/result.py
And the python code:
#! /usr/bin/env python
import rospy
from std_msgs.msg import Empty
...
The problem is that I get an error:
Traceback (most recent call last):
File "WebRoot/result.py", line 2, in <module>
import rospy
ImportError: No module named rospy
However, when I run the code manually via terminal, it works fine.
When you run it command line, you are probably getting a different environment from when it's running in your JVM. In your bash script, try pointing directly to the python version you intend to use. It's entirely possible that the JVM's env is pointing to a different version of python. Or that the environment is not fully setup.
Try to put the full path, like this, for example:
#!/bin/bash
/usr/bin/python2.7/python WebRoot/result.py
I'm trying to convert a CVS repository to Git using cvs2svn and am following the directions on this page. I got to step 7 but am getting an error running git-move-refs.py:
Traceback (most recent call last):
File "../../cvs2svn-trunk/contrib/git-move-refs.py", line 23, in ?
from subprocess import Popen, PIPE, call
ImportError: No module named subprocess
For reference, this is what the script shows:
usage = 'USAGE: %prog [options]'
import sys
import optparse
from subprocess import Popen, PIPE, call
I'm not a Python expert but from browsing the web it looks like subprocess is a standard module, right? I'm using a Python installation built from source for version 2.6.3. What am I missing for this script to work?
I'm guessing that you have an old version (pre-2.4) of Python at /usr/bin/python, from your distribution, and the Python 2.6 you compiled is somewhere else (like /usr/local/bin/python). You have the Python 2.6 executable on your path before /usr/bin, so when you execute python from the command-line you get Python 2.6.
However, looking at the cvs2svn source code, git-move-refs.py's interpreter line is hard-coded to
#!/usr/bin/python
instead of #!/usr/bin/env python, which means when you run the script it uses the old Python.
As a workaround, run the script by passing it to your Python 2.6 interpreter:
user#host$ python /path/to/cvs2svn/contrib/git-move-refs.py