If you want to do data analysis in python, you always need to use python packages like Numpy, Pandas, Scipy and Matplotlib, etc. All those python packages are so powerful and useful to do Base N-dimensional array computing( Numpy ), Data structures & analysis ( Pandas ), scientific computing ( Scipy ), and Comprehensive 2D Plotting ( Matplotlib ). But the first step is to install the related packages on your OS, this article will tell you how to install them on Windows, Mac, and Linux.
1. Install Numpy, Pandas, Scipy, Matplotlib With Anaconda.
- Anaconda is a python edition that is used in scientific areas, so if you install Anaconda, all the above packages will be installed automatically. So please read the article How To Install Anaconda On Linux, Windows, macOS Correctly to install anaconda first.
- After installation, you can run command conda in a terminal to list the above packages to make sure it has been installed correctly like below.
~$ conda list pandas # packages in environment at /home/zhaosong/anaconda3: # # Name Version Build Channel pandas 0.23.4 py37h04863e7_0
- To list all installed anaconda packages, just run the command
$ conda list
. - Run
$ conda -h
to list the conda command help information. - If you want to remove/uninstall a package, run
$ conda remove <package name>
2. Install Numpy, Pandas, Scipy, Matplotlib By PIP Command.
- First, make sure pip has been installed on your OS. If it is not installed, please refer article How To Install Python/Pip On Windows.
~$ pip --version pip 18.1 from /home/zhaosong/anaconda3/lib/python3.7/site-packages/pip (python 3.7)
- Run pip install command to install related packages.
pip install numpy pip install pandas pip install scipy pip install matplotlib
- Run pip uninstall command to uninstall related packages.
pip uninstall numpy pip uninstall pandas pip uninstall scipy pip uninstall matplotlib
- Run pip show command to display package install information.
~$ pip show pandas Name: pandas Version: 0.23.4 Summary: Powerful data structures for data analysis, time series, and statistics Home-page: http://pandas.pydata.org Author: None Author-email: None License: BSD Location: /home/zhaosong/anaconda3/lib/python3.7/site-packages Requires: python-dateutil, pytz, numpy Required-by: seaborn, odo
3. How To Install Correct Numpy, Scipy, Matplotlib Package For Multiple Python Versions.
- I have 2 python versions installed on my Ubuntu Linux OS, they are python 2.7 and python 3.8. The python 2.7 is a built-in python version when I installed Ubuntu. And I install python 3.8 manually.
- Now I want to install the Scipy library on my Ubuntu Linux OS, and I find the below command in scipy.org, then I run the below command in a terminal.
python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
- But when the above command execution was complete, I found it only install the NumPy, Scipy, Matplotlib, iPython, etc for python 2.7. It does not install the packages for python 3.8 as I want, how can I resolve this issue? Thanks a lot.
- This is because Ubuntu Linux installed python 2.7 by default, then the default pip command is also of python version 2.7.
- So when you run the
python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
command, it will use python 2.7’s pip command to install. - To fix this issue, you need to first install the pip command for python 3.8 (
sudo apt-get install python3-pip
), and then run the pip3 command to install all the Scipy packages for python 3.8 (python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
).
4. How To Fix The Error: Setup Script Exited With Pandas Requires Numpy >= 1.6 Due To Datetime64 Dependency During The Installation Of Python Pandas.
- I use macOS version 10.15 to develop a python program, and I want to install pandas in it.
- I run the command sudo easy_install pandas to install it, and it returns an error message like below.
error: Setup script exited with pandas requires NumPy >= 1.18 due to datetime64 dependency
- So I get my Numpy version by executing the command pip show numpy like below, I find the installed Numpy version is less than 1.18.
$ pip show numpy Name: numpy Version: 1.16.1 Summary: NumPy is the fundamental package for array computing with Python. Home-page: https://www.numpy.org Author: Travis E. Oliphant et al. Author-email: None License: BSD Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
- So I run the command sudo easy_install numpy to install the python NumPy package and its success.
- When I run the command sudo easy_install pandas to install the python pandas package again, the error message still exists.
- I finally find the below method to fix this error.
- Open a dos window, then run the command python to go to the python interactive console.
(MyPythonEnv) C:\Users\zhaosong>python Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
- Then run the below command to get the existing Numpy package installation directory.
>>> import numpy >>> print(numpy) <module 'numpy' from 'C:\\Users\\zhaosong\\anaconda3\\envs\\MyPythonEnv\\lib\\site-packages\\numpy\\__init__.py'>
- Go to the python Numpy module installed directory and remove the entire directory.
- Now you can install the latest version of Numpy with the command sudo easy_install numpy or sudo pip install numpy.
Practical 1: SDES
from Crypto.Cipher import DES
from secrets import token_bytes
key = token_bytes(8)
def encrypt(message):
cipher = DES.new(key , DES.MODE_EAX)
nonce = cipher.nonce
ciphertext
,tag=cipher.encrypt_and_digest(message.encode(‘ascii’
))
return nonce, ciphertext, tag
def decrypt(nonce , ciphertext,tag):
cipher=DES.new(key , DES.MODE_EAX , nonce=nonce)
plaintext = cipher.decrypt(ciphertext)
try:
cipher.verify(tag)
return plaintext.decode(‘ascii’)
except:
return False
pt = input(“Enter the Message -> “)
nonce,ciphertext,tag=encrypt(pt)
print(“\n\t\t\t\t\t– !! DES ENCRYPTION !! –“)
print(f”\n\tCipher Text of ‘{pt}’ ->
“+str(ciphertext))
plaintext = decrypt(nonce,ciphertext,tag)
if not plaintext:
print(“Corrupted Message”)
else:
print(“\n\tPlain Test Decrypted -> “+str(plaintext))
Practical 2: AES
from Crypto.Cipher import AES
key = b’SIXTEEN BYTE KEY’
def encrypt(message):
cipher = AES.new(key , AES.MODE_EAX)
nonce = cipher.nonce
ciphertext
,tag=cipher.encrypt_and_digest(message.encode(‘ascii’))
return nonce, ciphertext, tag
def decrypt(nonce , ciphertext,tag):
cipher=AES.new(key , AES.MODE_EAX , nonce=nonce)
plaintext = cipher.decrypt(ciphertext)
try:
cipher.verify(tag)
return plaintext.decode(‘ascii’)
except:
return False
pt = input(“Enter the Message -> “)
nonce,ciphertext,tag=encrypt(pt)
print(“\n———————————————————
——————————————————————
——————“)
print(“\n\t\t\t\t\t– !! AES ENCRYPTION !! –“)
print(f”\n\tCipher Text of ‘{pt}’ -> “+str(ciphertext))
print(“\n———————————————————
——————————————————————
——————“)
plaintext = decrypt(nonce,ciphertext,tag)
if not plaintext:
print(“Corrupted Message”)
else:
print(“\n—————————————————-
——————————————————————
——————-“)
print(“\n\tPlain Test Decrypted -> “+str(plaintext))
Practical 3: Diffie Hillman
import math
import time
print(“———————————————————————
—————————-“)
print(“\n\t\tInput”)
p = int(input(“\nEnter the Global Value – ‘p’ -> “))
g = int(input(“\nEnter the Global Value – ‘g’ -> “))
print(“\n\t\tPrivate Key Input”)
xa = int(input(“\nEnter the Private Key of user ‘A ‘ : ‘Xa’ -> “))
xb = int(input(“\nEnter the Private Key of user ‘B ‘ : ‘Xb’ -> “))
print(“\n——————————————————————-
——————————“)
ya= math.fmod((g**xa) , p)
yb= math.fmod((g**xb) , p)
print(“———————————————————————
—————————-“)
print(“\n\t\tOutput\t\t”)
print(“\n\tPrivate Keys\n”)
print(“\n\tPrivate key of user ‘A’ -> “+str(xa))
print(“\n\tPrivate key of user ‘B’ -> “+str(xb))
print(“\n\n\tPublic Keys”)
print(“\n\tPublic key of user ‘A’ -> “+str(ya))
print(“\n\tPublic key of user ‘B’ -> “+str(yb))
print(“\n——————————————————————-
——————————“)
print(“\n\t–!! Exchanging Public Keys !!–“)
time.sleep(3)
print(“\n——————————————————————-
——————————“)
ka= math.fmod((yb**xa) , p)
kb= math.fmod((ya**xb) , p)
print(“\n——————————————————————-
——————————“)
print(“\n\tSecret key of user ‘A’ -> “+str(ka))
print(“\n\tSecret key of user ‘B’ -> “+str(kb))
print(“\n——————————————————————-
——————————“)
if ka==kb :
print(“\n————————————————————-
————————————“)
print(“\n\t!!! The Seceret Keys are Verified and Sucessfully
Exchanged Public Keys !!!” )
print(“\n————————————————————-
————————————“)
Practical 4: RSA
import math
print(“\n———————————————————
–“)
print(“\n\t\t\t INPUT VALUES”)
p = int(input(“\n\tEnter First Prime NO. ‘p’ -> “))
q = int(input(“\n\tEnter Second Prime NO. ‘q’-> “))
print(“\n———————————————————
–\n”)
m = int(input(“\n\tEnter the Message to Cypher -> “))
print(“\n———————————————————
–\n”)
# cal n and phi(n)
n = p*q
phin = (p-1)*(q-1)
#for finding e
e= 0
for i in range(2,phin):
if((math.gcd(i,phin)==1)):
e= i
break
#for finding d
d = 1
for i in range(e,phin):
if((math.fmod((i*e),phin)==1)):
d = i
break
#ciypher text
c = int(math.fmod((m**e),n))
print(“\n\n\n\n—————————————————
——–\n”)
print(“\n\t\t\t Output VALUES”)
print(“\n\tFollowing is the Cypher Text ‘c’-> “,c)
print(“\n———————————————————
–\n”)
#decrypt
decrypt = int( math.fmod((c**d),n))
print(“\n\tFollowing is the decyphered message -> “,decrypt)
Practical 5: Elliptic Curve
from tinyec import registry
import secrets
import time
#For Display Purpose
def compress(pubKey):
return hex(pubKey.x) + hex(pubKey.y % 2)[2:]
#Curve Selection
curve = registry.get_curve(‘secp192r1’)
print(“\nThe Curve Used is -> secp192r1 “)
print(“\n———————————————————
———————————-“)
print(“\n\t\t\t PUBLIC KEYS”)
#User A
APrivKey = secrets.randbelow(curve.field.n) #na
APubKey = APrivKey * curve.g #PA = na * G
print(“\n\n\tUser ‘A’ public key:”, compress(APubKey))
#User B
BPrivKey = secrets.randbelow(curve.field.n) #nb
BPubKey = BPrivKey * curve.g #PB = nb * G
print(“\n\tUser ‘B’ public key:”, compress(BPubKey))
print(“\n———————————————————
———————————-“)
print(“\n\t\t\tNow exchange the public keys”)
time.sleep(3)
print(“\n———————————————————
———————————-“)
print(“\n\t\t\t Shared KEYS”)
#Display Shared Key ‘K’
ASharedKey = APrivKey * BPubKey #K = na * PB
print(“\n\tUser ‘A’ Shared key:”, compress(ASharedKey))
BSharedKey = BPrivKey * APubKey #K = nb* PA
print(“\n\tUser ‘B’ Shared key:”, compress(BSharedKey))
print(“\n———————————————————
———————————“)