Raspberry Pi Python Program First Line Error/Python Library Error - python

The first two lines of the program are:
from i2clibraries import i2c_lcd
from ABElectronics_ADCPi import ADCPi
No matter what line is first the Pi returns an error when I attempt to run it under Python or Python 3. All the libraries are possessed and registered. Using the shell commands the checks saying the exports worked correctly all show up correctly. However, whatever line is line 1 will return a missing module error and the i2clibraries will always return a missing module error. By keeping that as the first line I get the least errors in running, but the program still returns this:
pi#raspberrypi ~ $ sudo python file.py
Traceback (most recent call last):
File "file.py", line 1, in <module>
from i2clibraries import i2c_lcd
File "/home/pi/i2clibraries/i2c_lcd.py", line 1, in <module>
from i2clibraries import i2c
File "/home/pi/i2clibraries/i2c.py", line 1, in <module>
from quick2wire.i2c import I2CMaster, writing_bytes, reading
ImportError: No module named quick2wire.i2c
Given the error, what possible solutions are there to stop the first line from being unable to find its module?

Problem
The error message is telling you that when you try to import the i2clibraries module, the imports that it requires (dependencies) cannot be found when it is itself is being imported. This is specifically in the first line of i2c.py file - where the line
from quick2wire.i2c import I2CMaster, writing_bytes, reading
is failing.
The problem is almost certainly that your modules are not on the Python module search path. Further info on this is given at the end of this answer should you need it.
Solution
There are a number of ways to resolve this. The one recommended by the developers of the module is
To use the library without installation, add the full path of the
source tree to the PYTHONPATH environment variable. For example:
export QUICK2WIRE_API_HOME=[the directory cloned from Git or unpacked from the source archive]
export PYTHONPATH=$PYTHONPATH:$QUICK2WIRE_API_HOME
So, you need to know where your quick2wire libraries are installed - from your error message, I would hazard a guess that they are in /home/pi/i2clibraries/, so $QUICK2WIRE_API_HOME=/home/pi/i2clibraries/ should be your first line of the above pair.
Further info
You can read more generally about how to install modules on Python 2.x on the Python website. You can look at what paths make up the module search path by going to an interactive Python prompt (i.e. typing python) and then doing.
>>> import sys
>>> sys.path
This will output a list containing strings representing all the paths that will be searched for modules.

Related

python relative import from same package fails

I am working on a project involving the popular aiortc/aioquic github package. Located at src/aioquic/buffer.py there is an import statement:
from ._buffer import Buffer, BufferReadError, BufferWriteError # noqa
This line is meant to import a C/stub module _buffer.c / _buffer.pyi . When used from the root directory to run examples found in the examples directory this import line will work, but when trying to run in a Docker container from the same directory this import fails and when importing from the /src directory the import fails. I have tried reading several other questions on python relative imports but none seem to make sense to me.
To reproduce the issue:
open python in the src directory
from aioquic import about (works)
from aioquic import buffer (fail)
from aioquic import buffer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/aioquic/src/aioquic/buffer.py", line 1, in <module>
from ._buffer import Buffer, BufferReadError, BufferWriteError # noqa
ModuleNotFoundError: No module named 'aioquic._buffer'
I don't understand why the examples http3_client and http3_server work but when trying to import it directly it fails. I thought I understood roughly how to use python imports but apparently not. What would cause the same line to work when running from one directoy but fail when running from another directory? Is it something magical with the PYTHONPATH? I'm hoping someone smarter than I am can explain why this is failing.
[EDIT and most importantly how to fix this so the import will work nomatter where the user runs python from. I have tried several different combinations but none seem to work.]
Thanks in advance.
I figured out what was wrong.
The stub/C module had to be built into a .pyd file before it could imported. This was done by
pip install -e .
on the parent directory
Is it something magical with the PYTHONPATH?
Yes.
Typically . dot (current working directory) will appear
in that exported env var,
so where you started matters.
Take care to ensure you're using the same env var setting,
and the same pwd,
during both the interactive and the Docker runs.

python virtualenv cant seem to find modules even though they've already been installed

I am trying to run my python app on my website, and so I've created a virtual environment for all of the packages, and I've correctly installed all of the packages I need. When I check on the file path to my installed packages, they are all pointing to the correct path, and when I got to my site-packages file that they point to, they are all there.
However,
Every time I try to run my app, I get this:
stdin: is not a tty
Traceback (most recent call last):
File "/opt/cpanel/ea-ruby24/root/usr/share/passenger/helper-scripts/wsgi-loader.py", line 369, in <module>
app_module = load_app()
File "/opt/cpanel/ea-ruby24/root/usr/share/passenger/helper-scripts/wsgi-loader.py", line 76, in load_app
return imp.load_source('passenger_wsgi', startup_file)
File "/home/dsmproto/testblockchain/passenger_wsgi.py", line 1, in <module>
import main as application
File "/home/dsmproto/testblockchain/main.py", line 2, in <module>
from wallet import wallet
File "/home/dsmproto/testblockchain/wallet.py", line 1, in <module>
from Cryptodome.PublicKey import RSA
ImportError: No module named Cryptodome.PublicKey
I know that the module is there, but for some reason it is still saying there's no module.
Is there a command I can use in my SSH to check where my app is looking for modules?
All of the modules I have installed are installed correctly. Furthermore, when I open the SSH and run python commands in the terminal, they are able to import all of the modules without any issues.
So how come my app can't find these modules? Is there a way to check to make sure they are looking in the right place?
Thanks in advance for the help
EDIT:
I ran a script in python to look at the sys.path and got this:
/opt/alt/python38/lib64/python38.zip
/opt/alt/python38/lib64/python3.8
/opt/alt/python38/lib64/python3.8/lib-dynload
/home/dsmproto/virtualenv/testblockchain/3.8/lib64/python3.8/site-packages
/home/dsmproto/virtualenv/testblockchain/3.8/lib/python3.8/site-packages
Those are the correct folders that my packages are installed in, yet when I try to run my app, it still says it can't find them.

Pycharm "no module named REPORT.py" however it exist and works. But then

Pycharm says REPORT.py file doesn't exist. However the code works perfectly.
Here is the image of the issue:
Now that is not the strangest part. I know sometimes when you working within a package you have to reference the package name: import package_name.filename so when I tried that it appears to have fixed the incorrect reporting.
But then...
Traceback (most recent call last):
File "C:/Users/REDACTED/PycharmProjects/REDACTED/MAIN/MAIN.py", line 2, in <module>
import MAIN.REPORTS as PDD
File "C:\Users\REDACTED\PycharmProjects\REDACTED\MAIN\MAIN.py", line 2, in <module>
import MAIN.REPORTS as PDD
ModuleNotFoundError: No module named 'MAIN.REPORTS'; 'MAIN' is not a package
This seams like a mistake with how Pycharm is checking for my file.
For the sake of completeness I also tried to import from.
Pycharm is also not marking this as invalid but I get a new interesting error when running the code:
Traceback (most recent call last):
File "C:/Users/REDACTED/PycharmProjects/REDACTED/MAIN/MAIN.py", line 2, in <module>
from MAIN import REPORTS as PDD
File "C:\Users\REDACTED\PycharmProjects\REDACTED\MAIN\MAIN.py", line 2, in <module>
from MAIN import REPORTS as PDD
ImportError: cannot import name 'REPORTS'
As request in the comments here is my folder structure:
Per suggestion in the comments I did try to import * but I still get the same reporting issue from Pycharm.
Update:
I believe I found out why the issue existed when trying to import from my package.
Due to my package name being MAIN and my main py file being MAIN.py I believe my code was trying to import from the py file and not the package.
After renaming my package to MAIN_PACK and doing import MAIN_PACK.REPORT as PDD the code works fine without any reporting from Pycharm saying it is not valid.
That at the vary least fixes the Pycharm reporting.
However that still does not explain why Pycharm reports that import REPORTS is not a module but still the code would work.Does anyone know why this is occurring?
Set the main folder as the source root. You can do that by right clicking the MAIN folder and navigating to the bottom of the list. Mark the directory as source root. Go into Pycharm settings under console and check add source roots to python path.
Pycharm uses source root to resolve imports. More information can be found here
https://www.jetbrains.com/help/pycharm/content-root.html
Relevant text from link:
Source roots (or source folders; shown as the Source root icon ).
These roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports.
The files under the source roots are interpreted according to their type. PyCharm can parse, inspect, index, and compile the
contents of these roots.

Importing a python module to enable a script to be run from command line

I'm new to python and trying to test a script from this github repo (https://github.com/mgp25/psn-api).
The root directory has an example.py and I'm trying to run it with
$ python example.py
which gives this error:
Traceback (most recent call last):
File "example.py", line 1, in <module>
from src.Auth import Auth
ImportError: No module named src.Auth
How can I get this to run?
There is a folder in the root directory named src but because I'm new to python I don't know how to connect things so that the src.Auth module gets imported (or if that's even the right terminology)
Python 3.3+ will happily interpret it as a package without an __init__.py, fwiw, and I believe that's what the author wrote in.
Also note from trying to run it just now, you'll need to install simplejson and requests. (Normally there'd be a requirements.txt or similar saying this.)
Being in the repository root directory, you do:
touch src/__init__.py
This will create an empty file but it is necessary for the Python module search system. Then you should be able to run it without problems, unless there is some dependency on external libraries.

python "ImportError: cannot import name urandom"

Somehow my python is broken and emits the error:
jseidel#EDP15:/etc/default$ python -c 'import random'
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.6/random.py", line 47, in <module>
from os import urandom as _urandom
ImportError: cannot import name urandom
This is NOT the virtualenv error that is so commonly documented here and elsewhere: I don't use python directly, I have never setup a virtualenv explicitly, and there is no virtualenv directory or python script that I can find anywhere on my system.
I'm running Kubuntu 10.04 and until just recently my KPackageKit worked just fine and handled updates with no problem. Now it shows nothing... maybe because of this python error, maybe because of something else.
How do I go about finding the error and fixing python?
As suggested by #Armin Rigo, this worked for me:
1) Add a print 42 at the end of the /usr/lib/python2.6/os.py file.
2) If you see "42", then that is the correct os.py file and the urandom module is not include. Add the statement to include urandom (you can find a sample from another os.py file). This was what worked for me.
3) If you don't see "42", then that's not the os.py file that you're using. Find the random.py file that is crashing and insert import os; print os.__file__ to get more information about the failure.

Categories

Resources