Tut01 Python Installation Modified
Tut01 Python Installation Modified
Tut01 Python Installation Modified
Introduction to
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
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
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
• List packages:
$ pip list
$ pip list --outdated # these packages should be upgraded
$ pip list | grep numpy # on Linux or macOS
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
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
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
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.
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