What is the right way to use the libimobiledevice python bindings? - python

I am using macOS 10.15.7.
I use pyenv to install Python 3.8.6.
I use python -m venv myenv to setup a virtual environment and activate that environment.
I clone the liblist repo from https://github.com/libimobiledevice/libplist. I then use ./autogen.sh --prefix=/Users/jamesh/tmp/local --enable-debug to generate the Makefile. After make'ing and installing, I get the plist.so file.
$ pwd
/Users/jamesh/tmp/local/lib/python3.8/site-packages
$ ls
plist.a plist.la* plist.so*
I can cd into /Users/jamesh/tmp/local/lib/python3.8/site-packages, run ipython, and execute import plist. As it is importing, it crashes with 'ipython' terminated by signal SIGSEGV (Address boundary error)
I also tried a simple script with:
import sys
sys.path.append( "/Users/jamesh/tmp/local/lib/python3.8/site-packages" )
import plist
with the same result.
I figure I must be doing something wrong and cannot use the plist.so file directly.
What step am I missing?
However, I can do the basically the same on Ubuntu 18.04 and it works.

Related

Activating Python Virtual env in Batch file

I'm trying to make a batch script (called run_windows) that check if the python virtual environment exists and if not, create it then activate it, install the requirements and finally run some python code.
set "VIRTUAL_ENV=mat_visualizer_env"
:read_mat
%VIRTUAL_ENV%\Scripts\activate
pip install -r app_files/requirements.txt
python -c "import sys; sys.path.insert(1,'app_files'); from main import visualize_mat_eeg; visualize_mat_eeg('%1')"
pause
EXIT /B 0
IF EXIST "%VIRTUAL_ENV%\Scripts\activate.bat" (
CALL :read_mat
) ELSE (
pip install virtualenv
python -m venv mat_visualizer_env
CALL :read_mat
)
However, when I run my script, the code exits at line 4: %VIRTUAL_ENV%\Scripts\activate with no errors:
Few things:
Running the .bat or bash version of activate in powershell is not predictable, Activate.ps1 is found in newer releases. It could probably be used with older versions if copied from a newer release.
Scripts in windows (.bat or .cmd in windows) processes from top down, :<ANCHOR> is not skipped like a Sub or Function would be.
I have only worked with the Conda version of activate.ps1 and it adds some nice commands. Had a quick look in standard Python 3.10.5 and it does not add commands but should work fine.
Edit: Added that while working, is not the best option to use the bash version of activate
Here is a quick example of your batch file rearranged, and using the Call command as previously instructed.
#Echo Off
Set "VIRTUAL_ENV=mat_visualizer_env"
If Not Exist "%VIRTUAL_ENV%\Scripts\activate.bat" (
pip.exe install virtualenv
python.exe -m venv %VIRTUAL_ENV%
)
If Not Exist "%VIRTUAL_ENV%\Scripts\activate.bat" Exit /B 1
Call "%VIRTUAL_ENV%\Scripts\activate.bat"
pip.exe install -r app_files/requirements.txt
python.exe -c "import sys; sys.path.insert(1,'app_files'); from main import visualize_mat_eeg; visualize_mat_eeg('%~1')"
Pause
Exit /B 0
Please note, that there is no working directory defined in this script, as was yours, and therefore no way to guarantee that when run, the relative paths point to the intended locations. Additionally, you have not made it clear what %1 was supposed to be, or where it was coming from, so I cannot guarantee whether that is correct.

How to fix 'no module named requests'

I have requests module installed but it shows an error when running .py file from cmd prompt.
There are no errors when running the file from vscode.
It seems that requests is installed in your virtual enviroment "my_env" but you must execute your script using the intepreter of your virtual enviroment (It seems that windows keep using the base interpreter even if you activated the virtual env). Try calling it directly, for example
"Your_path_to_venv\Scripts\python.exe" main.py
This issue can be caused by multiple issues.
Wrong pip version is in path (Caused by installing a newer version of python, while an older version was already there.
This can be fixed by simply removing the old python from PATH and restarting the command line.
Try using pip3 instead of pip maybe pip installs libraries in a different dictionary than pip3 does
If that didn't work then use
python -m pip install “your library”
And if you were using python3 just do the same
python3 -m pip install ”your library”
If they did not work too, replace pip in the last two commands with pip3
And if it still does not work first see the path of the python site-packages files by running this python code
import os
import inspect
print(os.path.dirname(inspect.getfile(inspect))+"/site-packages")
a path will be printed, take it and add it as a parameter to your pip command
pip install -t “the printed path”
Ok, I see that you probably have a script called requests.py in your C:\Users\BokaBu>pyproj\my_env\Scripts\activate folder, but your question is how can C:\Users\BokaBu>pyproj\src\main.py find it.
The solution is to add C:\Users\BokaBu>pyproj\my_env\Scripts\activate to PYTHONPATH. The solution to the general version of your question can be found here:
Importing files from different folder - Stackoverflow
But in more detail, you may want to try this to your C:\Users\BokaBu>pyproj\src\main.py:
# In C:\Users\BokaBu>pyproj\src\main.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, 'C:\Users\BokaBu>pyproj\my_env\Scripts\activate')
import requests
Hope it helps~

OpenVino and PyInstaller on Raspberry PI 3B+

I tried to build a standalone executable with PyInstaller for Python 3.5 using OpenVino 2020.4.287.
PyInstaller assembled a file successfully but I received the next error after launch:
ImportError: No module named 'openvino'
I tried to include /opt/intel/openvino/deployment_tools/inference_engine/lib/armv7l/plugins.xml in data while building executable but it didn't help.
So the question is how to build a standalone executable in Raspbian with PyInstaller with OpenVino import?
Thanks.
Okay, the issue was resolved.
Initial conditions:
Raspbian Stretch with Python 3.5.
Considerations:
The last OpenVino supporting Python 3.5 is 2020.4.
So, let’s start. We will create a test.py file with the next lines:
import numpy as np
import openvino.inference_engine.constants
from openvino.inference_engine import IENetwork, IECore
print("start")
e = IECore()
print("end")
Now let's install OpenVino, Pyinstaller, create an executable, and run it:
sudo pip3 install pyinstaller
sudo mkdir -p /opt/intel/openvino
wget https://storage.openvinotoolkit.org/repositories/openvino/packages/2020.4/l_openvino_toolkit_runtime_raspbian_p_2020.4.287.tgz
sudo tar -xf l_openvino_toolkit_runtime_raspbian_p_2020.4.287.tgz --strip 1 -C /opt/intel/openvino
rm l_openvino_toolkit_runtime_raspbian_p_2020.4.287.tgz
sudo apt install cmake
source /opt/intel/openvino/bin/setupvars.sh
sh /opt/intel/openvino/install_dependencies/install_NCS_udev_rules.sh
pyinstaller --onefile --clean --add-data="/opt/intel/openvino/deployment_tools/inference_engine/lib/armv7l/plugins.xml:." test.py
dist/test
"start" and "end" messages should be shown.
The most important when you are using OpenVino 2020.4 is the next line:
import openvino.inference_engine.constants
In previous versions of OpenVino everything will work normally without this line. But in my specific case I’ve got the next error on OpenVino versions earlier 2020.4:
openvino.inference_engine.ie_api.IECore' object has no attribute 'read_network
That’s why I specified import for openvino.inference_engine.constants. Without this nothing works in OpenVino 2020.4.
Thanks.
The biggest possibility is you didn't run the setupvars before that implementation.
Please note that you need to ensure the setupvars had been run and initialized(you should see the init message) in each cmd or terminal before proceeding to any further inferencing/etc.
This setupvars would keep the required packages together. Hence, if you didn't run it, the problem like what you are facing would persist.

How to run Anaconda Python on sudo

Currently using AWS to run some tests on a machine learning project. I would like to run Python scripts without internet (via root) because the internet bandwidth is extremely limited. I try to run the convnets.py script by doing
sudo python convnets.py >> output
But that does not work, as Anaconda does not use PYTHONPATH, making it impossible for root to find the Anaconda Python environment. So errors like "cannot import" and "module not found" are thrown.
How do I set this up so I can get Anaconda and sudo to play fair together?
Because using sudo uses a different PATH than your typical environment, you need to be sure to specify that you want to use Anaconda's python interpreter rather than the system python. You can check which one is being run with the following command
sudo which python
To fix this, and point to Anaconda's python interpreter, specify the full path to the correct interpreter.
sudo /path/to/anaconda/bin/python convnets.py >> output
If you do this, you should be able to access all of the modules managed by anaconda.
On the other hand, if you have an Anaconda environment created
conda create --name $ENVIRONMENT_NAME python
You can activate it prior to running your command
sudo source activate $ENVIRONMENT_NAME && python convnets.py >> output

How can I run a python script using pythonbrew venv from the command line?

I recently came across this in a cron script at my place of work:
/bin/bash -c "[[ -s $HOME/.pythonbrew/etc/bashrc ]] && source $HOME/.pythonbrew/etc/bashrc && pythonbrew use 2.6.7 && pythonbrew venv use someapp && python /opt/someapp/bin/someapp.py"
This is for a system-wide (multi-user) installation of Pythonbrew.
It works. But please tell me there's a better way.
Addendum
To clarify what I'm looking for: I'd like a one-line command to run my script though a virtualenv tied to pythonbrew. With virtualenv alone, I could do something like this:
/opt/someapp/venv/bin/python /opt/someapp/bin/someapp.py
What I don't want is another script to run my script (like that cron command above).
I believe it can be done by using the python binary directly from you pythonbrew virtual environment.
By default its in ~/.pythonbrew/venvs/Python-<version>/<name of venv>/bin/python
But I think you can change the path with an environmental variable.
So just change the first half of the line you added to reference the pythonbrew virtual environment python binary and it should work.
On the first line on your python script add a shebang (#!) followed by a path to your target python. Then make the python script executable. It can then be executed directly from the command line (crontab, another bash script, whatever).
make a virtual env in your temp dir:
$ cd /tmp
$ virtualenv venv
the path to your python in that venv is /tmp/venv/bin/python
Using an editor create a simple script containing all of the following:
#!/tmp/venv/bin/python
print("hello world")
Save it in your home directory as "mypyscript.py"
make it executable:
$ chmod 755 mypyscript.py
Now you should be able to execute it using the filename directly on the command line:
$ ./mypyscript.py
hello world
Do this to your someapp.py substituting the relevant path to your python and that should work.
The trick turned out to be locating the pythonbrew virtualenv's python binary. Mark's answer pointed me in the right direction. But here's a complete rundown for future reference:
With pythonbrew installed, I did the following (as root on the server):
pythonbrew install 2.6.6
pythonbrew switch 2.6.6
pythonbrew venv create --no-site-packages myapp
I had a pip freeze file, so I set up my virtualenv using that:
/usr/local/pythonbrew/venvs/Python-2.6.6/myapp/bin/pip install -r /tmp/requirements.pip
Now my python binary can be found at /usr/local/pythonbrew/venvs/Python-2.6.6/myapp/bin/python. So to run my script:
/usr/local/pythonbrew/venvs/Python-2.6.6/myapp/bin/python /opt/myapp/bin/myapp.py

Categories