I'm trying to run the following commands in a Python script
import subprocess
image_name = "alpine:3.10"
scan_image = "trivy -q image -f json {}".format(image_name)
scan_result = subprocess.check_output(scan_image.split()).decode('utf-8')
If I run it from a Python script, it raises the following error: [Errno 2] No such file or directory: 'trivy'
But if I run the command using python interpreter (interactive mode) it works fine.
-bash-4.2# python3
Python 3.6.8 (default, Apr 2 2020, 13:34:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> image_name = "alpine:3.10"
>>> scan_image = "trivy -q image -f json {}".format(image_name)
>>> subprocess.check_output(scan_image.split()).decode('utf-8')
'[\n {\n "Target": "alpine:3.10 (alpine 3.10.5)",\n "Type": "alpine",\n "Vulnerabilities": null\n }\n]'
>>>
What could I be doing wrong? I've run the commands using Python2 and Python3 interpreters.
Security concerns aside, you need to provide the full path to your executable:
Replace trivy in the script with the results of which trivy from a shell
Related
I have a Python 2.7 question, if somebody can help.
When we install a Python module using pip, how do we make it available to all users? Please, see the example below (with module faker). The import works when I am root, but doesn’t work when I am ubuntu user.
I have already tried to install using option --system, and also changing umask, as recommended in some articles I have found. Didn’t work so far. Any ideas?
If we run "which python", both users point to the same one.
root#ip-172-30-244-157:/home/ubuntu#
root#ip-172-30-244-157:/home/ubuntu# python
Python 2.7.17 (default, Sep 30 2020, 13:38:04)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import faker
>>>
>>> exit()
root#ip-172-30-244-157:/home/ubuntu#
root#ip-172-30-244-157:/home/ubuntu#
root#ip-172-30-244-157:/home/ubuntu# exit
exit
ubuntu#ip-172-30-244-157:~$
ubuntu#ip-172-30-244-157:~$
ubuntu#ip-172-30-244-157:~$ python
Python 2.7.17 (default, Sep 30 2020, 13:38:04)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import faker
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named faker
>>>
Ok, I solved the issue.
In my case, the problematic module was "faker". But, when we install the faker, another additional module is installed as well (in this case - text-unidecode).
Then I uninstalled both modules, ran "umask 022" and re-installed the faker.
This solved the issue for all other users.
Thanks all for the help!
I am using virtualenv to run a script that uses subprocess popen to run another program that requires the system wide python and original environment variables. How do I prevent virtualenv from affecting it?
You can pass in a modified PATH for the subprocess with env=.
from subprocess import Popen
from os import environ
from os.path import join as path_join
myenv = environ.copy()
if 'VIRTUAL_ENV' in environ:
myenv['PATH'] = ':'.join(
[x for x in environ['PATH'].split(':')
if x != path_join(environ['VIRTUAL_ENV'], 'bin')])
Popen(['command', '--with', 'arguments'], env=myenv)
virtualenv creates a copy of python executable and you can activate it to your current shell:
This will change your $PATH so its first entry is the virtualenv’s
bin/ directory. (You have to use source because it changes your shell
environment in-place.) This is all it does; it’s purely a convenience.
If you directly run a script or the python interpreter from the
virtualenv’s bin/ directory (e.g. path/to/ENV/bin/pip or
/path/to/ENV/bin/python-script.py) there’s no need for activation.
So when I've activated python in a virtualenv for my project it's the one that will be used:
gonczor#wiktor-papu:~/Projects/papukurier/papukurier$ source ../venv/bin/activate
(venv) gonczor#wiktor-papu:~/Projects/papukurier/papukurier$ which python
/home/gonczor/Projects/papukurier/venv/bin/python
(venv) gonczor#wiktor-papu:~/Projects/papukurier/papukurier$ python
Python 2.7.14 (default, Sep 23 2017, 22:06:14)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'/home/gonczor/Projects/papukurier/venv/bin/python'
>>>
But at the same time you can give full path to execute any other python instance on your computer:
(venv) gonczor#wiktor-papu:~/Projects/papukurier/papukurier$ /usr/bin/python
Python 2.7.14 (default, Sep 23 2017, 22:06:14)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'/usr/bin/python'
>>>
I believe subprocess doesn't care about you venv
subprocess.run('/path/to/system/python program.py', stdout=PIPE, stderr=PIPE)
I installed 64bit package of cefpython in Ubuntu 12.04 LTS (http://code.google.com/p/cefpython/). The problem is I am not able to run the examples. It says no module named wx.
But when I navigate to the directory /usr/local/lib/python2.7/dist-packages/cefpython1 and do import wx it works. So basically I am not able to import wx outside that directory. I am using python interpreter on terminal.
rishi:cefpython1 ls
cefclient cefpython_py27.pyc chrome.pak examples __init__.pyc LICENSE.txt wx
cefpython_py27.py cefpython_py27.so devtools_resources.pak __init__.py libcef.so locales
rishi:cefpython1 python
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>>
[2]+ Stopped python
rishi:cefpython1 cd ..
rishi:dist-packages python
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named wx
>>>
KeyboardInterrupt
>>>
My PATH is as follows and PYTHONPATH is empty:
installed 64bit package of cefpython in Ubuntu 12.04 LTS (http://code.google.com/p/cefpython/). The problem is I am not able to run the examples. It says no module named wx. But when I navigate to the directory /usr/local/lib/python2.7/dist-packages/cefpython1 and do import wx it works. So basically I am not able to import wx outside that directory. I am using python interpreter on terminal.
rishi:dist-packages echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
rishi:dist-packages echo $PYTHONPATH
rishi:dist-packages echo $PYTHONPATH
rishi:dist-packages
Your problem here is that python has no idea where you are importing from. Refer to This post for instructions. Basically, what is happening is that when you're in the directory, python knows to look for it (it looks for python files and packages in the directory). You need to add the python libraries into the PYTHONPATH.
When I type "python" and return in shell, the following lines will come out:
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
How to surpress these lines please?
An easy way is to call Python as python -i -c "". This will also disable any start-up scripts, though. If you have a start-up script, you can also use python -i ~/.pythonrc.py (or however that script is named).
When I start Python from Mac OS' Terminal.app, python recognises the encoding as UTF-8:
$ python3.0
Python 3.0.1 (r301:69556, May 18 2009, 16:44:01)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'UTF-8'
This works the same for python2.5.
But inside Emacs, the encoding is US-ASCII.
Python 3.0.1 (r301:69556, May 18 2009, 16:44:01)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'US-ASCII'
How do I make Emacs communicate with Python so that sys.stdout knows to use UTF-8?
Edit: Since I don't have the rep to edit the accepted answer, here is precisely what worked for me on Aquaemacs 1.6, Mac OS 10.5.6.
In the python-mode-hook, I added the line
(setenv "LANG" "en_GB.UTF-8")
Apparently, Mac OS requires "UTF-8", while dfa says that Ubuntu requires "UTF8".
Additionally, I had to set the input/output encoding by doing C-x RET p and then typing "utf-8" twice. I should probably find out how to set this permanently.
Thanks to dfa and Jouni for collectively helping me find the answer.
Here is my final python-mode-hook:
(add-hook 'python-mode-hook
(lambda ()
(set (make-variable-buffer-local 'beginning-of-defun-function)
'py-beginning-of-def-or-class)
(define-key py-mode-map "\C-c\C-z" 'py-shell)
(setq outline-regexp "def\\|class ")
(setenv "LANG" "en_GB.UTF-8"))) ; <-- *this* line is new
check your environment variables:
$ LANG="en_US.UTF8" python -c "import sys; print sys.stdout.encoding"
UTF-8
$ LANG="en_US" python -c "import sys; print sys.stdout.encoding"
ANSI_X3.4-1968
in your python hook, try:
(setenv "LANG" "en_US.UTF8")