Tut01 Python Installation Modified

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

CSCI2040

Introduction to
Python

Python Installation Tutorial 1


Outline
1. Install conda and python
2. Package Management (conda, pip)
3. Environment Management (conda)
4. Coding in Python

2
3

Install Conda
(Highly recommended)
What is Conda and Why Conda
• Conda is an open source package management system and
environment management system that runs on Windows, macOS and
Linux.
• Conda quickly installs, runs and updates packages and their
dependencies.
• Conda easily creates, saves, loads and switches between
environments on your local computer.
Actually, conda has almost become the most popular tool for
data scientists and artificial intelligence engineers to manage
Python environments and packages.

4
We recommend Miniconda, which is a
lite version of Anaconda. It takes less

Install Miniconda (Windows) space but has the essentials of a data


scientist toolset. It is also okay if you
prefer Anaconda to Miniconda, which
• URL: https://docs.conda.io/en/latest/miniconda.html has more pre-installed packages in it.
• Download corresponding installers
• Install it (Remember to click the option “Add
Miniconda3 to my PATH env”!!! )
• (Ignore the “Not recommended” under it)

• If you want to use Anaconda:


https://www.anaconda.com/products/distributio
n
5
Install Miniconda (Windows) Cont’d
• Open CMD (press Win+R, type cmd, enter)
• Run “conda” or “python --version”

6
Install Miniconda (Mac and Linux)
• Open terminal (For Mac, Command + Space, Type “terminal”, Hit Enter;
For Linux, it depends on your distribution)

7
Install Miniconda (Mac and Linux) Cont’d
• Run the commands in your terminal (It’s also ok if you prefer Anaconda to Miniconda,
which has more pre-installed packages in it.)

# Linux:
wget -c https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

# MacOS:
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
bash Miniconda3-latest-MacOSX-x86_64.sh

• Then reopen your terminal

• If you want to install Anaconda On MacOS or Linux:


• https://docs.anaconda.com/anaconda/install/mac-os/
• https://docs.anaconda.com/anaconda/install/linux/ 8
Install Miniconda (Mac and Linux) Cont’d
• You should see (base) in
front of your prompt.
• Run “conda” or
“python --version”

9
They’re all set! Tips:
• pip (a python package manager) is already automatically installed if
you installed conda.
• If you are not sure about which one is being used, simply run have
multiple Python runtimes installed on your machine and you
“conda activate base”
to use the Python of the conda base environment. (We will introduce
the virtual env. mechanism of conda in the slides later.)

10
11

Package
Management
Using conda to Manage Your Python Packages
• Install packages:
$ conda install SomePackage # latest version
$ conda install SomePackage==1.0.4 # specific version
$ conda install 'SomePackage>=1.0.4' # minimum version
e.g.
$ conda install matplotlib # install a chart plotting tool

$ conda install numpy scipy # install multiple packages

• Uninstall packages: • List installed packages:


$ conda uninstall SomePackage
$ conda list
• Update packages:
$ conda update SomePackage
12
Using pip to Manage Your Python Packages
• Install packages:
$ pip install SomePackage # latest version
$ pip install SomePackage==1.0.4 # specific version
$ pip install 'SomePackage>=1.0.4' # minimum version
e.g.
$ pip install matplotlib # install a chart plotting tool

$ pip install numpy scipy # install multiple packages

• Uninstall packages: Note also the --user option if you have no


access permission to install packages in a system
directory (like /usr/local/lib/python3.4)
$ pip uninstall SomePackage
or if you don’t want to affect the system-wide
Python env. Ref: (see this post for more details)
13
Using pip to Manage Your Python Packages
• Upgrade a package:
or -U
$ pip install --upgrade somepackage

• List packages:
$ pip list
$ pip list --outdated # these packages should be upgraded
$ pip list | grep numpy # on Linux or macOS

• Show details of a package: kingtin@aist2602:~$ pip show scipy


Name: scipy
Version: 1.4.1
Summary: SciPy: Scientific Library for Python
Home-page: https://www.scipy.org
$ pip show SomePackage Author: None
Author-email: None
License: BSD
Location: /home/kingtin/.local/lib/python3.7/site-packages
Requires: numpy 14
15

Environment
Management
Using conda
• conda can be used to create isolated Python environments.
• It allows us to create independent environments for each of
our Python projects. So each project can have its own set of
dependencies, regardless of the dependencies in another project.
• Imagine you have an application that needs version 1 of LibFoo, but another
application requires version 2. How can you use both on the same host?
• Conda even considers your Python as a package, which means you
can use conda to manage multiple versions of Python
• Imagine you have an application that needs Python 2, but another application
requires Python 3. How can you use both on the same host?

16
Creating a Virtual Environment
• Create a virtual env

$ conda create --name <name> python=<version>

• For example, an env named csci2040 with python 3.9


$ conda create --name csci2040 python=3.9 Best practice: Give your virtual
environment a meaningful name,
• Enter it: e.g. dev (for development),
prod (for production)
$ conda activate csci2040 • List all env(s):
• Leave it:
$ conda env list
$ conda deactivate
17
Activating a Virtual Environment
• Before you can start installing or using packages in your virtual
environment, you’ll need to activate it, which means putting the
virtual environment-specific python and pip executables into your
shell’s PATH.

$ conda activate env

Your virtual environment’s name

18
Confirming You Are in a Virtual Environment
• You can confirm you’re in the virtual environment by checking the
location of your Python interpreter, it should be pointing to
the env directory.
• On macOS and Linux:
(env) $ which python
...conda3/envs/env/bin/python

• On Windows: Your virtual environment’s name


(env) $ where python
/c/Users/…/miniconda3/envs/env/python.exe
• Note: If you find which python (or where python) doesn’t work, please also try
which python3, or where python3
19
Leaving a Virtual Environment
• To leave your virtual environment, simply run:
(env) $ conda deactivate

• To re-enter the virtual environment, just follow the same instructions


above about activating a virtual environment. There’s no need to re-
create the virtual environment.

20
21

Integrated
Development
Environment
How to Program Using Python (REPL)
• To enter a REPL (Read Eval Print Loop) mode, in your terminal:
(env) $ python

• Then you can program in it.

22
How to Program Using Python (Script)
• Write some code in to a text file (you can use whatever editor you
like, but Windows Notepad is not recommended because of its weird
behaviour of processing UTF-8 files):
# Linux/MacOS
(env) $ echo 'print("I love python")' > hello.py
# Windows
(env) $ echo print("I love python") > hello.py
# Then:
(env) $ python hello.py

Recommended Editors:
Cross Platform: VSCode, Sublime Text
Windows: Notepad++, Notepad2
23
How to program using Python (IDE)
• Usually, editors only provide an interface for you to type your code. It
cannot compile and run your code. So, you need to type commands in
a terminal to compile and run a program.
• An IDE is more powerful, it has integrated the code compiler, runtime,
debugger and sometimes profiler into the GUI for more professional
coding experience.

• One suggested IDE: PyCharm is an excellent cross-platform IDE


(Integrated Development Environment) for Python programming

24
How to program using Python (IDE)
• The Professional Edition is free for academic users.
• You can register a free educational license using your CUHK email.
• The Community Edition is lighter and free for use.
• Download: https://www.jetbrains.com/pycharm/download/

25
Create Projects in Pycharm
• Choose your Interpreter:

26

You might also like