"ImportError: No module named serial" - after installing pyserial - python

I'm writing a project in python which should eventually running on LinkIt One IoT device.
I've written some test code to check whether I am able to connect between Arduino IDE to python (I am working with Pycharm).
the test code is:
import serial
import time
arduino = serial.Serial('COM1', 115200, timeout=.1)
time.sleep(1) #give the connection a second to settle
arduino.write("Hello from Python!")
while True:
data = arduino.readline()
if data:
print data.rstrip('\n')
when I am running to code I am getting:
C:\Users\אורי\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/אורי/PycharmProjects/untitled2/test.py
Traceback (most recent call last):
File "C:/Users/����/PycharmProjects/untitled2/test.py", line 1, in
import serial
ImportError: No module named serial

how did you install your serial module?
If you want to make sure It will be detected,
go into your console
pip install serial
and run your code from the console too
python test.py # make sure your console is in the right folder path
or
find where the module is installed, something like "C:\Python27\Lib\site-packages"
import sys
sys.path.append("C:\Python27\Lib\site-packages") # this is where python stores modules, yours could be different
import serial

Sometimes we mix things up ourselves. But we won't realize that.
On linux or windows :
A = Os required Python version(like:64bit or 32Bit, 2.7.x, 3.X.X, etc.)
B = The version we need.(like:x86 arch, 1.6.X, 2.6.X, 3.0, etc.)
C = Another version we need(like:X64)
Every IDLE (which is linked to /bin or OS.PATH) work on native directory (os never touch this things.). Install a library system-wide but try to call external IDLE or install library locally and call from system-wide.
Which installer (I do not recommend) used system environment variables, but maybe you want install a library locally!
Short answer:
Go to your idle directory(what you want(used))
Download your module
Extract somewhere
Use version of Python (which you want, joined dir(1.)) python /extract-dir/setup.py *kwargs
Note :
import sys
sys.path.append("C:\Python27\Lib\site-packages")
Don't do that, because you aren't standard user !
I hope it helps. I'm sorry if you lost time.

Related

python library loads and works properly from command line, but 'ImportError: No module' if I try to run at boot

I’m trying to run a couple of python programs at boot time (in connection with some halloween costumes I’ve been making with my kids; I don’t want somebody to have to log in remotely to start the software in their costumes if they have to reboot for some reason).
The software works perfectly if run from the command line, whether launched from its own directory or any other. However if I try to run at launch by putting the script in rc.local, nothing happens and I log the following error:
Traceback (most recent call last):
File "/home/pi/sound.py", line 4, in <module>
from pad4pi import rpi_gpio
ImportError: No module named pad4pi
Additional information:
Some additional color: which python returns '/usr/bin/python’ and I have fully specified in rc.local that this is the python to use (/usr/bin/python /home/pi/sound.py &)
I installed with pip install pad4pi, and checking a moment ago it confirms that
pad4pi in ./.local/lib/python2.7/site-packages (1.1.5)
Why would the pad4pi library (which is needed in both projects I’m working on) be available from the command line but not at launch? And more importantly, what can I do about it?
[Note, solution below. I will accept the answer when stackOverflow lets me.]
The library was not appearing in $Pythonpath at boot time.
I added
export PYTHONPATH=$PYTHONPATH:/home/pi/.local/lib/python2.7/site-packages to my rc.local file, before it ran my python code, and all was well.

Module ImportError in running Python Script in JMeter

I would like to use python scripts in Jmeter, so I can check the Load and the performance of an application...for that I've created a Thread with JSR223 Sampler, and I put the content of python script, and after the run, Jmeter throws error like : File "", line 8, in
ImportError: No module named opcua, ModuleNotFoundError: No module named 'dateutil'
see snapshot:
I don't know how to install this modules (package)in Jmeter, with PyCharm, I installed the package in the Project Interpreter under the setting see the snapshot...
You can do something like:
Install required module(s)
jython -m pip install foo
Add the next line(s) to the beginning of your script:
import sys
sys.path.append('\\location\\of\\jython\\site-packages')
However from performance perspective it will be much better to re-write your script in Groovy as calling Jython interpreter is not the best choice when it comes to high loads.
Also be aware that you can invoke Python interpreter as "external" process using JMeter's OS Process Sampler (if amount of the existing Python code is too big or too complex to rewrite in Groovy), see How to Run External Commands and Programs Locally and Remotely from JMeter for more details.

Trying to import mcpi.minecraft on my Mac but I keep getting errors

I have recent acquired the book adventures in Minecraft and want to give it a try on my Mac. I follow the instructions until it tells me to type this code into my python 2.7:
import mcpi.minecraft as minecraft
mc = minecraft.Minecraft.create()
mc.postToChat("Hello Minecraft World")
I keep receiving a error stating that no module named mcpi.minecraft exists. I checked the folder that stores my program and it does have an mcpi folder. Does anybody have the solution?
I had the same issue. I ended up not even using IDLE. I just write the commands into a separate terminal window form the one I am running the server on.
I have not written any scripts yet and tried to run them in this way but I am sure it works.
If you use the terminal rather than IDLE (like me) I would pip install mcpi
I use
from mcpi.minecraft import Minecraft
mc = Minecraft.create()
I also decided to read this book and faced the same problem. You just need to save the file by the path: / AdventuresInMinecraft / MyAdventures here you save your file.

Can't Open Python IDLE

Getting the following code opening in the console in 9 separate screens when I try to open Python IDLE. As far as I'm aware, there isn't anything that I have done to the original install. Any ideas? Tried uninstalling and reinstalling but no luck. The code shown when I open the program is...
try:
import idlelib.PyShell
except ImportError:
# IDLE is not installed, but maybe PyShell is on sys.path:
try:
from . import PyShell
except ImportError:
raise
else:
import os
idledir = os.path.dirname(os.path.abspath(PyShell.__file__))
if idledir != os.getcwd():
# We're not in the IDLE directory, help the subprocess find run.py
pypath = os.environ.get('PYTHONPATH', '')
if pypath:
os.environ['PYTHONPATH'] = pypath + ':' + idledir
else:
os.environ['PYTHONPATH'] = idledir
PyShell.main()
else:
idlelib.PyShell.main()
I only have the option to close this window and not to create a new Python file.
I am an Idle maintainer and it is very unclear how you tried to open Idle, what you mean by '9 screens', and what you 're-installed', and on what system and version of Python.
The easiest way to start Idle with installed Python is to use the Start menu icon (on Windows) or the equivalent on other systems. On a command line, use python -m idlelib, where python starts some version of installed 3.x. For uninstalled 3.x or 2.x, use idlelib.idle. From within Python, import idlelib.idle starts Idle.
If you have python 2.7 or 3.4 properly installed and running, so that python -m test.regrtest runs and passes (.regrtest is only needed for 2.x), I would be very surprised if Idle did not run with the methods above.
The code you posted is idlelib/idle.pyw. It is used by idlelib/idle.bat and may have other specialized uses, such as running Idle with a subprocess call. I suspect that it is partially obsolete. In any case, I doubt it will be helpful to you.

Can't get Python IDLE to recognize OGR/GDAL Module

Folks,
Just getting started using OGR and Python for a variety of geospatial tasks. I'm working outside of OSGEO4w, and have installed GDAL w/ Python Bindings as well as Python v. 2.7.8 on my machine.
That said, I can run python and import gdal from a command-line interface but am unable to import the module when I use the IDLE environment. It returns an error telling me that the module doesn't exist. My install must be sound given that it works in the cmd prompt, so what's the deal?
This is all new to me, I'd appreciate any help or advice ya'll can impart; all previous questions I've read were concerned w/ merely installing (which I've done successfully) and getting Python to recognize the module from the command prompt, which it does.
Cheers,
Mike
Mike,
You can open IDLE with your python installation from the cmd line through:
>>>from idlelib import PyShell
>>>PyShell.main()
This should open IDLE from your current python, and you should be able then to import gdal and ogr from there.
Alternatively, you should have a windows batch file here
C:\YOURPYTHONPATH\Lib\idlelib\idle.bat
Running this should achieve the same.
Martin
Consider creating the following batch where PyInst references the Python installation folder from which IDLE is launched (i.e. idle.bat) and QgisInst references the folder containing "bin\o4w_env.bat":
#echo off
set PyInst=C:\Python27
set QgisInst="C:\Program Files\QGIS Brighton"
call %QgisInst%\bin\o4w_env.bat
set PYTHONPATH=%PyInst%\DLLs;%PyInst%\Lib;%PyInst%\Lib\lib-tk
set TCL_LIBRARY=%PyInst%\tcl\tcl8.5
python %PyInst%\Lib\idlelib\idle.pyw

Categories