Hot answers tagged

39

To run a set of Python commands from a bash script, you must give the Python interpreter the commands to run, either from a file (Python script) that you create in the script, as in #!/bin/bash -e # Create script as "script.py" cat >script.py <<'END_SCRIPT' print("TESTPRINT") END_SCRIPT # Run script.py python script.py rm script.py (this ...


22

Python is not mandatory for Linux, and there are plenty of small "embedded" Linux systems that don't have it. However, many distributions require it. So RHEL may have a dependency on Python because some of their management tools and scripts have been written in it. On those systems python is a requirement.


17

Python isn’t necessary, and it’s easy to prove: a basic Debian installation can get on the network etc. but doesn’t have Python. Python is an interpreter like any other. It can be required in some distributions, if their core tools (package managers etc.) are written in Python; other interpreters are required without anyone making a fuss about it (Perl for ...


17

You are using the Forsyth PD Korn shell, the usual login shell on OpenBSD. The PD Korn shell does not have a source command. The source built-in command is only available in some shells. The command that you want is the . command. Further reading What is the difference between '.' and 'source' in shells?


17

In addition to Kusalananda's answer, if you want the entire script to be run by python you can just change the first line to #!/usr/bin/env python3 and run it like any normal shell script. That way you don't have to remember what script you have to run with which interpreter.


10

The source keyword which is available in bash is not part of the Posix standard. Instead you can use . myVenv/bin/activate You could use the same syntax with . in bash which you are using on your Linux system.


9

Your title is fully answered by A do nothing line in a bash script: : or true are effectively equivalent to pass. However in these circumstances I would flip the condition: if ! grep something then code fi and >>> for element in a: ... if element: ... print element


9

I'm not going to nitpick on the awful terminology, but yes, GNU sed with its -i ("in-place") flag could be safely used by more than one process at the same time without any extra locking, because sed is not actually modifying the file in-place, but it's redirecting the output to a temporary file, and if everything goes well, it will rename(2) (move) the ...


7

Simple solution is edit .bashrc and put this line: alias python3=python3.7 Whenever you will write python3 it will replace it with python3.7. Or you can use command update-alternatives which is preferred i.e: sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/...


7

To fix this problem, you should reinstall Python 2.7 shipped with CentOS 7. (See below as for why you should reinstall, and how you should manage newer versions of software in a distro such as CentOS.) Reinstalling older Python from RPM Since yum is not working, you'll have to do most of it manually, by downloading the packages and reinstalling them using ...


7

The following warning in packaging.python.org may answer you questions Warning Recent Debian/Ubuntu versions have modified pip to use the “User Scheme” by default, which is a significant behavior change that can be surprising to some users.


7

You may also try the <<< (Here Strings) operator to save on lines: $ python <<< 'print("MyTest")' MyTest


6

Create symlink for /usr/bin/python3. In my LinuxMint: # ls -lh /usr/bin/python3 /usr/bin/python lrwxrwxrwx 1 root root 9 ноя 24 2017 /usr/bin/python -> python2.7 lrwxrwxrwx 1 root root 9 сен 6 2017 /usr/bin/python3 -> python3.5 # mv /usr/bin/python # cp /usr/bin/python3 /usr/bin/python # python --version Python 3.5.2


6

using these commands can help you: check the version of python: ls /usr/bin/python* alias: alias python='/usr/bin/pythonxx' re-login: . ~/.bashrc check the python version again: python --version


6

The simplest solution would be to use timeout from the collection of GNU coreutils (probably installed by default on most Linux systems): timeout 10 ./sopare.py -l See the manual for this utility for further options (man timeout). On non-GNU systems, this utility may be installed as gtimeout if GNU coreutils is installed at all. Another alternative, if ...


6

With awk: $ awk -v FS="\t" '$1 ~ /Group/ { file = $2 } { print $0 > file }' input.txt awk checks if there is Group in the first column. If this is the case we store the value of the second column in the variable file. After checking this, the whole line is printed and redirected by > to a file with the name stored in the variable file.


6

If your jq has the inputs function, and assuming {#PROCNAME} is just a string, you can use the following: ps axho comm --sort -rss | head -5 | jq -Rn '{data: [inputs|{"#PROCNAME":.}]}' The inputs functions lets jq read all input string. The rest is decoration to get the wanted format. The option -R gets raw string as input. The option -n feeds jq input ...


5

You correctly referenced the variable here: echo "Fetching from ${url}" So do the same thing here: some_var=$(python somePythonScript.py "${url}")


5

You can’t install pywin32 on Kali Linux because it’s only available for Windows. It provides access to the Win32 APIs and to COM objects, and a Python program intended to work on any Linux distribution shouldn’t need those (or it should only attempt to use them conditionally). You could always try running Python under Wine but I suspect that would involve ...


5

The env utility will execute its argument as a command. If the argument is not given with a path, as in env python, the command will be looked up in $PATH. This is usually what you want when using env in the #!-line of a script. If the argument is given with a path, it will execute the command at that path. In your #!-line, you execute a specific Python ...


5

In Debian, pip3 defaults to the user scheme when run outside virtual envs, and files are installed under ~/.local. If you want to install a tool system-wide using pip3 on Debian and derivatives, you can still use pip3 install --system ... to use the system scheme. (It’s worth checking whether the tool in question is available as a Debian package; Debian ...


4

You can achieve this by applying below simple steps - Check python version on terminal - python --version Get root user privileges. On terminal type - sudo su Write down the root user password Execute this command to switch to python 3.6 - update-alternatives --install /usr/bin/python python /usr/bin/python3 1 Check python version - python --version Done.


4

This is a problem that keeps bugging me, and this is the only post I can find on the interwebs that queries this issue. I'm not sure it's working at all. Debdelta is checking which section it's in (main, backports or stable-security), and using a separate debdeltas.debian.net URI for each section. The script is failing at the line: delta_uri_base=...


4

TL;DR: Use the following script to clean your $XDG_DATA_HOME/$HOME from left-overs and unmount previous unionfs: #!/bin/sh set -eu echo Killing currently running Dwarf Fortress instances killall -q -9 Dwarf_Fortress || true echo Removing old Dwarf Fortress unionfs mounts and mount points find /tmp/ -maxdepth 1 -name "dwarf-fortress*" \ -printf " Found %...


4

It isn't necesarry in terms that the system won't function if it isn't there but some distros have tools that are written in Python. Anaconda, which is the installer for RHEL, CentOS, Fedora, Oracle Linux, and Scientific Linux, is written in Python as are yum and dnf which are the package managers. They also require Python to function so if something is ...


4

Try this, find / -name '*.py' -exec grep -l "keyword" {} \; Explanation: find / -name '*.py': Find files below / with py extension. -exec grep -l keyword {} \; Within the files found, grep for keyword and output the filename instead of the match -l. I'm not familiar with Mac OS, but if you have globstar option in your shell, you can use the following: ...


4

No such file or directory: ' /home/ryan/Desktop/Art.jpg' Note the leading whitespace in the quoted pathname in the error message. If that's actually included in the variable that contains the pathname, and not just an extra space in the python command that outputs the error message, the error happens because the pathname the program is looking for is ...


4

The error isn’t caused by apt itself, but by apt-listchanges; disabling that will avoid the problem (apt isn’t Python-based): sudo mv /etc/apt/apt.conf.d/20listchanges{,.bak} This won’t fix your Python problems, so you might run into other errors. You shouldn’t need to set PYTHONPATH and PYTHONHOME in /etc/environment, I recommend removing those lines too. ...


4

$ sort file.txt \ | grep -f <(cut -d' ' -f1 file.txt | sort | uniq -d) \ | awk 'last && last != $1 { printf "\n" }; { last=$1 ; print}' 542ed609dfc4d0cae44c4b7be6d66382 mba.txt 542ed609dfc4d0cae44c4b7be6d66382 tyrion final.txt 5ee434a2ebcf4c3c98ee07e9c1efddc0 foo.txt 5ee434a2ebcf4c3c98ee07e9c1efddc0 imac.txt (Thanks to "cas" for the awk ...


3

pip is not reset when you create a new virtual environment. When you create a new virtual environment, venv performs a new installation of pip and setuptools in this environment. Where does the pip version come from? When you run python -m venv, the installation of pip is a responsibility of the module ensurepip which bootstraps a new pip installation. You ...


Only top voted, non community-wiki answers of a minimum length are eligible