ModulesNotFound Error when running python3 in shell script - python

I have my python3 path under /usr/bin, any my pip path under /.local/bin of my local repository.
With some pip modules installed, I can run my code successfully through python3 mycode.py.
But I tried to run the shell script:
#!/usr/bin
echo "starting now..."
nohup python3 mycode.py > log.txt 2>&1 &
echo $! > pid
echo "mycode.py started at pid: "
cat pid
and my python code:
mycode.py
#!/usr/bin/env python3
from aiocqhttp import CQHttp, Event, Message, MessageSegment
...
...
...
It gives me:
Traceback (most recent call last):
File "mycode.py", line 2, in <module>
from aiocqhttp import CQHttp, Event, Message, MessageSegment
ModuleNotFoundError: No module named 'aiocqhttp'
I tried to replace the interpreter of the shell script with my local path, but it doesn't help. How should I run my python codes using the shell script?

In your case, the aiocqhttp was installed in your home directory, and when you invoke the script by sudo, python3 will not be able to know to search in your home (because it is root that is running the process). You may solve it using these two approaches:
Method 1:
sudo -H pip3 install -U aiocqhttp
Install this module system-wide so that every user can find it.
Method 2:
sudo PYTHONPATH=/home/a495488027/.local/lib/python3.8/site-packages python3 mycode.py
Temporarily add the python module search path to sudo and run the code. This is when you doesn't want to install the package system-wide.
Back to your problem,
sudo PYTHONPATH=/home/a495488027/.local/lib/python3.8/site-packages bash script.sh
should do the work.

Quick solution: install the same that you have installed with sudo and then it will be accesible for all users

Related

Trying to setup nfcpy, Returns "ModuleNotFoundError: No module named 'ndef'"

I am trying to setup a PN532_NFC_HAT on a Raspberry Pi with nfcpy but I am having problems with modules.
What I have done so far:
I have installed nfcpy on my Raspberry Pi by simply doing this in the terminal.
$ pip install -U nfcpy
And I have verified the installation with this command
$ python -m nfc --search-tty
And so far it works, nfcpy can find my NFC Reader
Then I have cloned the examples from github
with the command
pi#raspberrypi: git clone https://github.com/nfcpy/nfcpy.git
and entered the examples-folder
pi#raspberrypi:~/nfcpy/examples $
What happens:
nfcpy has some example programs but when I try to run them they return various errors.
If I for an example run this command on one of the examples, Tagtool.py
python3 tagtool.py
I get this
Traceback (most recent call last):
File "tagtool.py", line 29, in <module>
import ndef
ModuleNotFoundError: No module named 'ndef'
Other examples return errors about not finding the module named nfc.
What am I doing wrong?
Please try below ↓
pip uninstall nfcpy
pip3 install nfcpy
or
sudo pip3 install nfcpy

Jenkins - pip having long shebang line: No such file or directory error

I have a jenkins job in which I am using virtualenv and then installing some python modules (enlisted in a requirements file).
My code is pretty simple:
virtualenv env
source ./env/bin/activate
echo $(python --version)
echo $(which pip)
echo $(pip --version)
When my job runs, which python and which pip show me correct output but pip --version throws an error:
++ which python
/scratch/jenkins/ncs-jenkins/workspace/proj/docker/mail/tools/ici/gate/kokilla-gold/testproj-my deploy-newchanges-20/env/bin/python
++ which pip
/scratch/jenkins/ncs-jenkins/workspace/proj/docker/mail/tools/ici/gate/kokilla-gold/testproj-my deploy-newchanges-20/env/bin/pip
++ pip --version
/tmp/hudson2230168319717284464.sh: /scratch/jenkins/ncs-jenkins/workspace/proj/docker/mail/tools/ici/gate/kokilla-gold/testproj-my deploy-newchanges-20/env/bin/pip: /scratch/jenkins/ncs-jenkins/workspace/proj/docker/mail/tools/ici/gate/kokilla-gold/testproj-my deploy-newchanges-20/: bad interpreter: No such file or directory
I think what's going on here is that the shebang line is greater then 127 chars and that's causing the issue. Its also referenced here. My path is
PATH=/scratch/jenkins/ncs-jenkins/workspace/proj/docker/mail/tools/ici/gate/kokilla-gold/testproj-my deploy-newchanges-20/env/bin:/usr/local/bin:/usr/bin
/scratch/jenkins/ncs-jenkins/workspace/proj/docker/mail/tools/ici/gate/kokilla-gold/testproj-mydeploy-newchanges-20/env/‌​bin/pip is a text file. Here are its contents:
#!/scratch/jenkins/ncs-jenkins/workspace/proj/docker/mail/tools/ici/gate/kokilla-gold/testproj-mydeploy-newchanges-20/env/bin/python
# -*- coding: utf-8 -*-
import re
import sys
from pip import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())
I'm not sure what I can do to change the shebang line in pip because its autogenerated and stored in my Jenkins workspace when I run virtualenv. What can I do to prevent this error?

Running fabric script throws ImportError: No module named fabric.main

I am on Redhat, and when I run any fabric scripts I see the following error:
Traceback (most recent call last): File "/usr/bin/fab", line 8, in
from fabric.main import main ImportError: No module named fabric.main
The file /usr/bin/fab is configured to use python 2.7 (/usr/local/bin/python):
#!/usr/local/bin/python
# -*- coding: utf-8 -*- import re import sys
from fabric.main import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())
The result is the same even if I just call fab.
Not sure what else I should be configuring. I have not set up a virtualenv for fab. If I must, I will do so.
I installed python 2.7, and then I installed fab as follows:
wget https://bootstrap.pypa.io/get-pip.py
sudo /usr/local/bin/python get-pip.py
sudo /usr/local/bin/pip install fab
I ended up doing the following:
Install Python 2.7 in a virtualenv (~/virtualenvs/py2.7) by following the top-rated answer from Is it possible to install another version of Python to Virtualenv? by DTing
Install pip in ~/virtualenvs/py2.7/bin/:
wget https://bootstrap.pypa.io/get-pip.py
sudo ~/virtualenvs/py2.7/bin/python2.7 get-pip.py
Install fab in ~/virtualenvs/py2.7/bin:
sudo ~/virtualenvs/py2.7/bin/pip install fab
For some reason, I still did not have the fab file under ~/virtualenvs/py2.7/bin, so I just copied the original /usr/bin/fab that was giving me issues to ~/virtualenvs/py2.7/bin/ and edited it to point to the virtualenv python2.7 (~/virtualenvs/py2.7/bin/python2.7)
Running ~/virtualenvs/py2.7/bin/fab worked, and gave me the following (welcome) error:
Fatal error: Couldn't find any fabfiles!
Remember that -f can be used to specify fabfile path, and use -h for
help.
Aborting.
I am content for now -- since fab seems to work. But if anyone has ideas why the actual fab file was not created in the ~/virtualenvs/py2.7/bin/ directory, I am all ears. Thank you!

ImportError: No module named distutils

Attempt to install psutils resulted a big headache...
$ python -V
Python 2.4.2
$ cat /etc/SuSE-release
SUSE Linux Enterprise Server 10 (x86_64)
VERSION = 10
PATCHLEVEL = 4
$ cd psutil-2.1.1/
$ python setup.py install
Traceback (most recent call last):
File "setup.py", line 17, in ?
from distutils.core import setup, Extension
ImportError: No module named distutils.core
Next - I try to install setuptools to use easy_install:
$ which easy_install
which: no easy_install
$ cd ../setuptools-1.4/
$ python setup.py install
Traceback (most recent call last):
File "setup.py", line 12, in ?
from distutils.util import convert_path
ImportError: No module named distutils.util
Trying install distutils from ez_setup.py:
$ python ez_setup.py
Traceback (most recent call last):
File "ez_setup.py", line 278, in ?
main(sys.argv[1:])
File "ez_setup.py", line 210, in main
egg = download_setuptools(version, delay=0)
File "ez_setup.py", line 139, in download_setuptools
from distutils import log
ImportError: No module named distutils
So - how can I install it?
P.S. No, I haven't root on this machine and can't use package manager.
you need to run this (if Error happens on python3) ==> sudo apt-get install python3-distutils --reinstall
you need to run this (if Error happens on python2) ==> sudo apt-get install python2-distutils --reinstall
I have an answer here but I will copy it here
AskUbuntu answer:
Debian has decided that distutils is not a core python package, so it is not included in the last versions of debian and debian-based OSes. You should be able to do sudo apt install python3-distutils and it should work.
However, it did not work for me. I use Parrot OS, which is, as Ubuntu, Debian based. I upgraded my system and pip stopped working for python3.7, and I also got the error ModuleNotFoundError: No module named 'distutils.util'
I tried a lot of stuff to fix it and to reinstall distutils, and I found out by pure luck, that pip3, for python3.8 did work. I then tried python3.7 -m pip3 -V, got /usr/bin/python3.7: No module named pip3 so I decided to have a look in the /usr/lib files.
I looked at /usr/lib/python3/dist-packages and everything looked fine. Then I looked at /usr/lib/python3.7 and saw the folder distutil.
I opened it, and saw the __pycache__, the __init__.py file and a version.py file. I had no idea how many files should be in there, or what the code should be, but I knew that those two files were either wrong or missing another file.
Then I had a look at what was inside /usr/lib/python3.8/distutil and it was totally different. I found the following files:
command Folder
__pycache__ Folder
archive_util.py Python script
bcppcompiler.py Python script
cmd.py Python script
config.py Python script
core.py Python script
cygwinccompiler.py Python script
debug.py Python script
dep_util.py Python script
errors.py Python script
extension.py Python script
fancy_getopt.py Python script
filelist.py Python script
file_util.py Python script
__init__.py Python script
log.py Python script
msvc9compiler.py Python script
_msvccompiler.py Python script
msvccompiler.py Python script
README Plain text file
spawn.py Python script
sysconfig.py Python script
text_file.py Python script
unixccompiler.py Python script
util.py Python script
version.py Python script
versionpredicate.py Python script
This was a lot more promising, and since pip3 did work, I assumed that this distutils worked too, and I tried to copy it to the python3.7 folder by running this command:
sudo cp -r /usr/lib/python3.8/distutil /usr/lib/python3.7/distutil
Then I tried again python3.7 -m pip -V and got
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.7)
Then I tried installing some modules and everything works fine.
I hope this is helpful.
#ciro
I need to do even more to get virtualenv running again (upgraded from 18.04 to 20.04):
sudo cp /usr/lib/python3.8/_sysconfigdata__* /usr/lib/python3.6/
cd /usr/lib/python3.6
sudo ln -s _sysconfigdata_m_linux_x86_64-linux-gnu.py _sysconfigdata_m_x86_64-linux-gnu.py

Installing Python setup tools on Unix to run Tweepy Twitter client for Python

I am trying to run Tweepy client for Twitter on my Unix account. whenever i try to run setup for Tweepy using the command:
python setup.py
I get this error:
Traceback (most recent call last):
File "setup.py", line 3, in ?
from setuptools import setup, find_packages
ImportError: No module named setuptools
Now i searched on some forums and found i need to add setup tools file. The file i found
setuptools-0.6c11-py2.7.egg
I FTP - ed this file to my unix directory where i have Tweepy client directory and my program which is using Tweepy.
Now, whenever i try to install setup tools using the command
python setuptools-0.6c11-py2.7.egg
I get the error :
python setuptools-0.6c11-py2.7.egg
File "setuptools-0.6c11-py2.7.egg", line 2
if [ `basename $0` = "setuptools-0.6c11-py2.7.egg" ]
^
SyntaxError: invalid syntax
Any clues/suggestions what i must be doing wrong here ?
Don't use setuptools, use distribute. Setuptools is old and deprecated. Until python 3.4 with packaging/distutils2 is around use distribute, which is a fork of old setuptools/distutils.
Simply download the distribute source tarball, unpack and run python setup.py install. Alternatively, you can download distribute-setup.py and just run it.
To install tweepy, run inside a virtualenv:
(venv) $ pip install tweepy
To create a virtual environment and install setuptools/distribute/easy_install, pip, and virtualenv, run:
$ curl -O https://raw.github.com/pypa/virtualenv/master/virtualenv.py
$ python virtualenv.py venv
$ source ./venv/bin/activate
taken from here
The setuptools documentation suggest using sh setuptools-0.6c11-py2.7.egg instead of python setuptools-0.6c11-py2.7.egg.
You get a SyntaxError from python because setuptools-0.6c11-py2.7.egg begins as a shell script:
#!/bin/sh
if [ `basename $0` = "setuptools-0.6c11-py2.7.egg" ]
then exec python2.7 -c "import sys, os; sys.path.insert(0, os.path.abspath('$0')); from setuptools.command.easy_install import bootstrap; sys.exit(bootstrap())" "$#"
else
echo $0 is not the correct name for this egg file.
echo Please rename it back to setuptools-0.6c11-py2.7.egg and try again.
exec false
fi
PK\00\00\00\00F\A3\E...

Categories

Resources