multipledispatch ModuleNotFoundError running from command-line - python

Running a locust (locust.io) script from the command line.
locust calls main.py which has the following imports:
from locust import HttpUser, between, task
from StreamLoader.stream_generator import * # thought this brings in everything
Packer.py has these imports:
from multipledispatch import dispatch
from PackedItem import PackedItem
StreamGenerator.py has:
import hashlib
from StreamLoader.Packer import Packer
from aes_encryption import AesEncryption
I used pip to install multipledispatch and when I run from within PyCharm it works fine, but from the command line I get the following.
File "C:\Users\guyl\PycharmProjects\engine-load-tests\engine_load_tester_locust\main.py", line 2, in <module>
from StreamLoader.stream_generator import *
File "C:\Users\guyl\PycharmProjects\engine-load-tests\StreamLoader\stream_generator.py", line 2, in <module>
from StreamLoader.Packer import Packer
File "C:\Users\guyl\PycharmProjects\engine-load-tests\StreamLoader\Packer.py", line 1, in <module>
from multipledispatch import dispatch
ModuleNotFoundError: No module named 'multipledispatch'
Here is what I have tried so far:
Add directories to the PYTHONPATH environment variable
Add empty __init__.py files in each package
This all seems unnecessary if I actually pip installed the module, though.
Answer below caused me to no longer see the error with multipledispatch. However, I now see a missing module error:
File "C:\Users\guyl\PycharmProjects\engine-load-tests\engine_load_tester_locust\main.py", line 2, in <module>
from StreamLoader.stream_generator import *
File "C:\Users\guyl\PycharmProjects\engine-load-tests\StreamLoader\stream_generator.py", line 2, in <module>
from Packer import Packer
ModuleNotFoundError: No module named 'Packer'
For clarity, I am running the code from locust which calls the Python code as depicted here. [Moderators - this question is getting quite long. Is that all right?]

PyCharm uses the virtual environment automatically but when you run from the command line the virtual environment isn't turned on.
Just follow the steps:
cd your_working_directory
environment_name/Scripts/activate if on Windows where environment_name is the name of the PyCharm virtual environment
Or environment_name/bin/activate if on MacOS
-------------EDIT------------------------
To answer the new question, try using pip freeze then see which packages are installed. Then install any dependencies which you want which are missing.

Related

ModuleNotFoundError: No module named 'rospkg'

I am new to ROS,
I have a problem when I import rospy into my script file example.py and run it: It says:
Traceback (most recent call last):
File "/home/nagarjunv/hk_ws/src/rvo/src/example.py", line 4, in <module>
import rospy
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/__init__.py", line 49, in <module>
from .client import spin, myargv, init_node, \
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/client.py", line 52, in <module>
import roslib
File "/opt/ros/melodic/lib/python2.7/dist-packages/roslib/__init__.py", line 50, in <module>
from roslib.launcher import load_manifest # noqa: F401
File "/opt/ros/melodic/lib/python2.7/dist-packages/roslib/launcher.py", line 42, in <module>
import rospkg
ModuleNotFoundError: No module named 'rospkg'
I am importing the following into my script file:The line 4 is rospy
#!/usr/bin/env python3
import sys
import rospy
THis is my CmakeLists:
cmake_minimum_required(VERSION 3.0.2)
project(rvo)
find_package(catkin REQUIRED COMPONENTS
rospy
std_msgs
message_generation
)
generate_messages(
DEPENDENCIES
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES rvo
CATKIN_DEPENDS rospy std_msgs
# DEPENDS system_lib
)
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
This is my package xml:
<buildtool_depend>catkin</buildtool_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<build_export_depend>message_generation</build_export_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>message_runtime</exec_depend>
I specify both in CMakelists and Package XML 'rospy' and importing it. I also checked my ros has rospy package available. BUt, I dont understand why I am having trouble running my script file?
rosrun rvo example.py
I will be glad to provide more details about this error. Can any please let me know the solution to this problem?
Try to change the shebang (line 1) according to the python version your ROS distro works with.
For melodic or earlier: #!/usr/bin/env python2
For noetic or later: #!/usr/bin/env python3
Depending on your ROS version and respectively the one of rospkg you may need to stick to python 2.x or 3.x.
Your script is calling python3 interpreter
#!/usr/bin/env python3
but you can clearly see in the error log that a different version is used by your installation:
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/init.py",
line 49, in
from .client import spin, myargv, init_node, \
If you want to use Python 3 for a ROS installation that is based on Python 2.7 you need to build everything from scratch but even then the chances for success are pretty slim.
Here are some possible solution:
If you want to use Python 3, see here. According to this article ROS Noetic is targeting only Python 3.
If you don't want to upgrade, you need to stick to Python 2.7.
If the version of Python is of no concern, just adapt your script to use the one your ROS installation is depending on.

Script works in Anaconda Prompt but not via Command Line

I am unable to run a python script in the command line, and this script works great in Jupyter Notebook and via Anaconda Prompt. This appears to be an issue importing the ssl module during initialization of another module I am importing (mygeotab).
I have googled the error and done as much as I can to diagnose the most common cause which appears to be PATH issues.
I have already diagnosed the PATH to a point, and have added the location of the /lib/ and python.exe to the environment variables. Also, during testing I began my script with the below to protect myself from path issues and printed the path before and after the 'append', which did not have an impact on the problem.
import sys
print(sys.path)
sys.path.append('C:\\Users\\xxxxxx\\Python Scripts')
sys.path.append('C:\\Anaconda3\\python37.zip')
sys.path.append('C:\\Anaconda3\\DLLs')
sys.path.append('C:\\Anaconda3\\lib')
sys.path.append('C:\\Anaconda3')
sys.path.append('C:\\Anaconda3\\lib\\site-packages')
sys.path.append('C:\\Anaconda3\\lib\\site-packages\\win32')
sys.path.append('C:\\Anaconda3\\lib\\site-packages\\win32\\lib')
sys.path.append('C:\\Anaconda3\\lib\\site-packages\\Pythonwin')
sys.path.append('C:\\Anaconda3\\lib\\site-packages\\IPython\\extensions')
sys.path.append('C:\\Users\\xxxxxx\\.ipython')
sys.path.append('C:\\Anaconda3\\Lib')
sys.path.append('C:\\Anaconda3\\Lib\\site-packages')
print(sys.path)
import mygeotab
import pandas as pd
import pyodbc as py
from mygeotab.ext import feed
import sqlalchemy
from time import sleep
However, when I attempt to run the script via the standard command line, I get the below error:
Traceback (most recent call last):
File "PYTHON_GEOTAB_TRIP_FEED.py", line 33, in <module>
import mygeotab
File "C:\Anaconda3\lib\site-packages\mygeotab\__init__.py", line 9, in <module>
from .api import Credentials
File "C:\Anaconda3\lib\site-packages\mygeotab\api.py", line 14, in <module>
import ssl
File "C:\Anaconda3\lib\ssl.py", line 98, in <module>
import _ssl # if we can't import it, let the error propagate
ImportError: DLL load failed: The specified module could not be found.
The mygeotab module and ssl.py are both in the locations designated in the Traceback. '_ssl' is the only reference I can not seem to diagnose. Again, this works fine in both Notebook and Anaconda Prompt.
Any ideas?
Windows Server 2008 R2
Anaconda 2019.07 x64
Python 3.7.3 x64
This was solved for me by installing a separate instance of Python 3.7, moving the PATH references and other pointers. I installed pip, mygeotab, and the other packages into the native Python 3.7 instance. It just appears you can't use the one baked into anaconda the way I thought. Thanks for the help everyone.

ImportError: No module named 'sqlite3'

I am trying to import nltk in my python file but i am getting this error
File "mycode.py", line 5, in
from utilities import TextCleaner, TF_IDF_FeatureExtraction File
"/home/myhome/Baseline/utilities.py", line 1, in import nltk
File
"/home/myhome/.local/lib64/python3.5/site-packages/nltk/init.py",
line 152, in from nltk.stem import * File
"/home/myhome/.local/lib64/python3.5/site-packages/nltk/stem/init.py",
line 29, in from nltk.corpus.reader.panlex_lite import *
File
"/home/myhome/.local/lib64/python3.5/site-packages/nltk/corpus/reader/panlex_lite.py",
line 15, in
import sqlite3 ImportError: No module named
'sqlite3'
The python version on server is 3.5.3 and i have sqlite version 3.13.0 installed
i am currently running code on remote server and i cant use sudo command since its restricted for remote users. is there any thing i can do without sudo command to solve this problem?
I Solved this issue by commenting out
import sqlite3 in the panlex_lite.py file present inside nltk library folder and also commented out sqlite3 connection string present inside this file and the code works now. This solution will only work if you are intented to use nltk only but not sqlite3
What you have installed on server, is not a python module, but the sqlite3 utility. If you have pip3 installed, you can run pip3 install pysqlite3 from user, so it will install the module sqlite3 in your home directory.
I assume by commenting out the import sqlite3 you are not allowing it to use the module, The issue is that the version of SQLite including with the OS is old. I have already highlighted the solution at the following discussion: No module named _sqlite3
You can find as well my blog post on the topic at: http://www.virtualizationteam.com/cloud/running-vcd-cli-fail-with-the-following-error-modulenotfounderror-no-module-named-_sqlite3.html

ImportError: No module named 'board' (AdaFruit)

I run the setup from this website to get my arduino to use AdaFruit LEDs.
And also run:
sudo pip3 install adafruit-circuitpython-neopixel
I then made this python code:
import board
import neopixel
pixels = neopixel.NeoPixel(board.D18, 12, brightness=0.2)
pixels[0] = (255, 0, 0)
And then executed it with python filename.py
And got the error:
ImportError: No module named 'board'
I then thought I maybe need to use python3 (Because it every where says too)
So I typed:
python3 light-test.py
This time got some more output, but in the end again an ImportError:
File "light-test.py", line 2, in <module>
import neopixel
File "/usr/local/lib/python3.5/dist-packages/neopixel.py", line 34, in
<module>
from neopixel_write import neopixel_write
File "/home/pi/.local/lib/python3.5/site-packages/neopixel_write.py", line
15, in <module>
from adafruit_blinka.microcontroller.raspi_23 import neopixel as _neopixel
File "/home/pi/.local/lib/python3.5/site-
packages/adafruit_blinka/microcontroller/raspi_23/neopixel.py", line 3, in
<module>
import _rpi_ws281x as ws
ImportError: No module named '_rpi_ws281x'
So I don't know know what I am doing wrong.
You're right to use Python3 instead of Python2.
One solution suggested here is to build the rpi_ws281x code from source.
Before that, however, you could try running as a super user/ administrator as suggested here.
If you're missing the 'board' module, that gets installed with the following pip3 command:
sudo pip3 install adafruit-blinka
I found that there was a board module already installed on my system (the wrong one). I deleted the board module and reinstalled adafruit-blinka to fix this.

Python include Scrapy from subdirectory

I would like to know if there's a way that I can put Scrapy into a subdirectory and import it. I did this with BeautifulSoup, rather than installing it, I just drop the bs4 directory into the directory of my app, and import it:
from bs4 import BeautifulSoup
In the source that I downloaded from scrapy.org there is no scrapy.py so I tried importing
from scrapy import *
This returned a bunch of errors.
Traceback (most recent call last):
File "C:\Users\Kat\Desktop\linkscrape\cookie.py", line 1, in <module> from scrapy import *
File "C:\Users\Kat\Desktop\linkscrape\scrapy\__init__.py", line 27, in <module>
from . import _monkeypatches
File "C:\Users\Kat\Desktop\linkscrape\scrapy\_monkeypatches.py", line 2, in <module>
from six.moves import copyreg
ImportError: No module named six.moves
Is there any way that I can simply include this to make it easy to migrate the app from computer to computer, or does this have to be installed? Thanks.
Don't do that. You will hurt yourself.
To achieve easy portability, use virtualenv - it's a golden standard of Python development.
Create a file named requirements.txt in a root directory of your project, and write all necessary dependencies, like this:
python-dateutil==2.4.2
Scrapy==1.0.4
XlsxWriter==0.7.7
When you are setting up the environment, create a fresh virtualenv and then simply:
pip install -r requirements.txt
VoilĂ ! You have a working environment.
In case of Scrapy, it's even more important because in production Scrapy is typically deployed using scrapyd, where having a correct package with a version number and fixed requirements is absolutely necessary.

Categories