How can I check if a package is installed and install it if not?
To check if packagename
was installed, type:
dpkg -s <packagename>
You can also use dpkg-query
that has a neater output for your purpose, and accepts wild cards, too.
dpkg-query -l <packagename>
To find what package owns the command
, try:
dpkg -S `which <command>`
For further details, see article Find out if package is installed in Linux and dpkg cheat sheet.
Checking for installed packages and if not found install
Try the following code :
if ! rpm -qa | grep -qw glibc-static; then
yum install glibc-static
fi
or shorter :
rpm -qa | grep -qw glibc-static || yum install glibc-static
For debian likes :
dpkg -l | grep -qw package || apt-get install package
For archlinux :
pacman -Qq | grep -qw package || pacman -S package
How to check if a module is installed in Python and, if not, install it within the code?
EDIT - 2020/02/03
The pip
module has updated quite a lot since the time I posted this answer. I've updated the snippet with the proper way to install a missing dependency, which is to use subprocess
and pkg_resources
, and not pip
.
To hide the output, you can redirect the subprocess output to devnull:
import sys
import subprocess
import pkg_resources
required = {'mutagen', 'gTTS'}
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = required - installed
if missing:
python = sys.executable
subprocess.check_call([python, '-m', 'pip', 'install', *missing], stdout=subprocess.DEVNULL)
Like @zwer mentioned, the above works, although it is not seen as a proper way of packaging your project. To look at this in better depth, read the the page How to package a Python App.
How do I compare two string variables in an 'if' statement in Bash?
For string equality comparison, use:
if [[ "$s1" == "$s2" ]]
For string does NOT equal comparison, use:
if [[ "$s1" != "$s2" ]]
For the a
contains b
, use:
if [[ $s1 == *"$s2"* ]]
(and make sure to add spaces between the symbols):
Bad:
if [["$s1" == "$s2"]]
Good:
if [[ "$s1" == "$s2" ]]
R check list of packages, then load or install
One way you could achieve this is by checking if it is installed using the installed.packages()
function. Then, you can also check which packages are loaded by using the .packages()
function.
When you implement this, it'd look like this:
# Declare packages
packages <- c("dplyr","ggplot2","magrittr")
# Loop through each package
for (package in packages) {
# Install package
# Note: `installed.packages()` returns a vector of all the installed packages
if (!package in installed.packages()) {
# Install it
install.packages(
package,
dependencies = TRUE
)
}
# Load package
# Note: `.packages()` returns a vector of all the loaded packages
if (!package in .packages()) {
# Load it
library(
package,
character.only = TRUE
)
}
}
Check if module exists, if not install it
Here is how it should be done, and if I am wrong, please correct me. However, Noufal seems to confirm it in another answer to this question, so I guess it's right.
When writing the setup.py
script for some scripts I wrote, I was dependent on the package manager of my distribution to install the required library for me.
So, in my setup.py
file, I did this:
package = 'package_name'
try:
return __import__(package)
except ImportError:
return None
So if package_name
was installed, fine, continue. Else, install it via the package manager which I called using subprocess
.
Elegant way to check for missing packages and install them?
Yes. If you have your list of packages, compare it to the output from installed.packages()[,"Package"]
and install the missing packages. Something like this:
list.of.packages <- c("ggplot2", "Rcpp")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
Otherwise:
If you put your code in a package and make them dependencies, then they will automatically be installed when you install your package.
Python - package not found although it is installed
You have (at least) 2 Python installations, one managed by Anaconda, the other what appears to be an official Python.org Mac build installed system-wide. The pip
command on the command-line is the one tied to the Python.org Mac build.
pip
is a script that is tied to a specific Python installation, and there can be multiple versions of the script installed in different locations, and is usually also installed with pipX
and pipX.Y
to match the X.Y
version indicator of the Python version it is tied to. For Python 3.6, that means the same script would also be available as pip3
and pip3.6
. (This also means that pip
can be connected to Python 2 or Python 3, depending on your exact OS setup. It is not a given that pip
, without a version number, installs into Python 2.x as some answers may claim).
Note that when you run a command without a path in your shell, (such as pip
as opposed to /usr/bin/pip
), you are asking your shell to find the command for you in a number of locations, listed in the PATH
environment variable. The first location in the PATH
list with that command is then fixed. which -a <command>
would tell you all possible PATH
-registered locations that the command can be found in. You can always use the full path to a command to bypass the PATH
search path.
You can always verify what Python version the pip
command is connected to with:
pip -V
which will output the version of pip
and the location it is installed with. It'll print something like
pip pipX.pipY path/to/pythonX.Y/site-packages/pip (python X.Y)
where pipX.pipY
is the pip version number and path/to/pythonX.Y
tells you what Python installation this is for.
You can try to match this with the Python version by running
python -m site
which outputs the Python module search path for that Python version. Python can be run with python
, pythonX
and pythonX.Y
too, and is subject to the same PATH
search.
Note the -m
switch there, that instructs Python to find a module in it's module search path and execute it as a script. Loads of modules support being run that way, including pip
. This is important as that helps avoid having to search for a better pip
command if you already can start the right Python version.
You have several good options here:
Since you are using Anaconda, you could look for a conda package for the same project. There is such a package for
wfdb
. Install it withconda install wfdb
Anaconda aims to give you a wider software management experience that includes a broader set of software options than just the Python PyPI ecosystem, and conda packages usually manage more things than just the Python package.
Conda packages are usually maintained by a different set of developers from the package itself, so there may be a newer version available on PyPI (requiring
pip install
) than there is on Conda.This is not an option for all Python packages, if there is no
conda
package you have to usepip
. See Installing non-conda packages.you can use the
conda
command to create a conda environment. Once you have an environment created, you can activate it withsource activate <name_of_cenv>
to alter your
PATH
settings. With the envirnoment 'active' the first directory listed on yourPATH
is the one for the conda environment and thepip
command will be the one tied to that environment.Note that a conda environment gives you an isolated environment for a specific project, keeping the library installation separate from the rest of your Python packages in the central
site-packages
location. If you want to install a package for all of your Anaconda Python projects, don't use a conda environment.Use the Anaconda Python binary to run
pip
as a module; when you can run/path/to/python
orpythoncommand
to open the right Python version, you can use that same path to run/path/to/python -m pip ...
instead ofpip ...
to be absolutely certain you are installing into the correct Python version.Look for a better
pip
command, withwhich -a pip
orwhich -a pip3.6
, etc. But if you already know the Python binary, look in the samebin
location forpip
. If you haveanaconda/bin/python
, then there probably is aanaconda/bin/pip
too.
Related Topics
How to Delete History of Last 10 Commands in Shell
Graphical Diff Programs for Linux
How to Connect Github Desktop with Cpanel
Any Way to Exit Bash Script, But Not Quitting the Terminal
Excluding Directory When Creating a .Tar.Gz File
How to Setup the Passwords-Less Authentication Between Two Different Accounts
Trailing Arguments with Find -Exec {} +
Adding a Header into Multiple .Txt Files
I Want to Remove Multiple Line of Text on Linux
Difference Between Cgroups and Namespaces
Add Up a Column of Numbers at the Unix Shell
How to Install Cargo on a Rhel Linux Server
Why Do I Get 'Permission Denied' After Using ./File2 in Linux
Loading Module on Keyboard Hotplug
Python Module Import Error for One User But Not the Other
Why Does Perf Stat Show "Stalled-Cycles-Backend" as <Not Supported>