Blog

A) Checking if a Python Package is already installed 

This article illustrates the method in Python to check if a Package is already installed or not.

Note : Below Code is tested from Jupyter Notebook 

# Code snippet to check if a Package is already installed or not

import importlib.util
import sys

# For illustrative purposes.
package_name = 'random'

spec = importlib.util.find_spec(package_name)

if spec is None:
    print(package_name , " : is not installed")
else :
    print(package_name , " : already installed")

B) Installing a Python Package 

Note : Below Code is tested from Jupyter Notebook

# Install a conda package in the current Jupyter kernel
import sys
!conda install --yes --prefix {sys.prefix} numpy

You  might get an error while executing the above script in Jupyter notebook , in that case follow the following steps to fix the error / issue -

Step 1 - type the following commands into your anaconda prompt

where python
where conda

Step 2 - Next, you can add Python and Conda to your path by using the setx command in your 
command prompt (replace C:UsersmgalarnykAnaconda2 with the results you got when running 
where python and where conda).

SETX PATH "%PATH%;C:UsersmgalarnykAnaconda2Scripts;C:UsersmgalarnykAnaconda2"

Congrats , the issue must get resolved now . Close the anaconda prompt & go back to Jupyter notebook .
Now execute the Script for Installing Package.

@AUTHOR : Admin

Tags:Eco, Water, Air, Environment

Comments (0)

    No Comments Found
Leave a Comment