Skip to content
Snippets Groups Projects
Commit 24e307f8 authored by Sven Graupner's avatar Sven Graupner
Browse files

add A_setup_python, Cygwin_setup.md, update .gitignore

parent 274b7676
No related branches found
No related tags found
No related merge requests found
...@@ -128,8 +128,8 @@ dmypy.json ...@@ -128,8 +128,8 @@ dmypy.json
# Pyre type checker # Pyre type checker
.pyre/ .pyre/
# project-specific files # project-specific files
README_init.md README_init.md
A_setup_python/
B_explore_python/ B_explore_python/
Cygwin_setup.md
# Assignment A: Setup Python &nbsp; (<span style="color:red">12 Pts</span>)
This assignment will setup your base Python enviroment. If you already have it, simply run challenges and answer questions (if any). If you cannot run challenges, set up the needed software.
### Challenges
1. [Challenge 1:](#1-challenge-1-terminal) Terminal
2. [Challenge 2:](#2-challenge-2-python3) Python3
3. [Challenge 3:](#3-challenge-3-pip) pip
4. [Challenge 4:](#4-challenge-4-test-python) Test Python
5. [Challenge 5:](#5-challenge-5-python-built-in-functions)
Python built-in functions
&nbsp;
### 1.) Challenge 1: Terminal
If you are using *MacOS* or *Linux*, skip steps for *Windows*.
For *Windows*,
- Use a Unix emulator such as
[cygwin](https://www.cygwin.com),
the built-in Windows Subsystem for Linux
[WSL](https://learn.microsoft.com/en-us/windows/wsl/install)
or a
[Linux VM](https://ubuntu.com/tutorials/how-to-run-ubuntu-desktop-on-a-virtual-machine-using-virtualbox).
- Windows *CMD.EXE* is no option. *Powershell* is not recommended since it is not compatible with Unix standards.
- Follow
[instructions](../Cygwin_setup.md)
for setting up *cygwin*.
Open a terminal and type commands:
```sh
> ls -la
> pwd
> whoami
> cat ~/.profile
> cat ~/.bashrc
> echo $PATH
```
Explain commands. If you are not familiar, find out about these basic Unix shell (bash, zsh, ...) commands (e.g. from [introduction](https://cs.lmu.edu/~ray/notes/bash) or
[tutorial](https://linuxconfig.org/bash-scripting-tutorial-for-beginners)).
On *Mac*, refer to file *.zshrc* instead of *.bashrc*.
(4 Pts)
&nbsp;
### 2.) Challenge 2: Python3
Check if you have Python 3 installed on your system. Name three differences between [Python 2 and 3](https://www.guru99.com/python-2-vs-python-3.html#7).
Run commands in terminal (exact version 3.x.x may vary):
```sh
> python --version
Python 3.12.0
```
(2 Pts)
&nbsp;
### 3.) Challenge 3: pip
Check if you have a Python package manager installed (pip, conda, ... ). [`pip`](https://pip.pypa.io) is Python's default package manager needed to install additional python packages and libraries.
Follow [instructions](https://pip.pypa.io/en/stable/installing) for installation:
- [download](https://bootstrap.pypa.io/get-pip.py) the `get-pip.py` file.
- run `python get-pip.py`
- or update pip to latest version: `python -m pip install --upgrade pip`
Run commands in terminal:
```sh
> pip --version
pip 23.2.1 from C:\Users\svgr2\AppData\Local\Programs\Python\Python312\Lib\site-
packages\pip (python 3.12)
```
(2 Pts)
&nbsp;
### 4.) Challenge 4: Test Python
Test Python:
```py
> python
Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print('Hello World')
Hello World
>>> help('modules')
...lists installed python packages
>>> 2+3*4
14
>>> x = 2+3*4
>>> x
14
```
Create a file *print_sys.py* with following content.
```py
import platform
impl = platform.python_implementation()
ver = platform.version()
mach = platform.machine()
sys = platform.system()
print('Python impl: ' + impl)
print('Python version: ' + ver)
print('Python machine: ' + mach)
print('Python system: ' + sys)
print('Python version: ' + platform.python_version())
```
Run the file. Output varies depending on your system.
```
> python print_sys.py
Python impl: CPython
Python version: 10.0.19045
Python machine: AMD64
Python system: Windows
Python version: 3.12.0
```
(2 Pts)
&nbsp;
### 5.) Challenge 5: Python built-in functions
Learn about Python's [built-in functions](https://docs.python.org/3/library/functions.html). Test the [*globals()*](https://docs.python.org/3/library/functions.html#globals) function.
```py
>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_
importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module
'builtins' (built-in)>, 'fruits': ['apple', 'pear', 'orange', 'banana']}
```
Test the [*input()*](https://docs.python.org/3/library/functions.html#input) function.
```py
>>> s = input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"
exit()
```
(2 Pts)
import platform
impl = platform.python_implementation()
ver = platform.version()
mach = platform.machine()
sys = platform.system()
print('Python impl: ' + impl)
print('Python version: ' + ver)
print('Python machine: ' + mach)
print('Python system: ' + sys)
print('Python version: ' + platform.python_version())
# Cygwin setup on *Windows*
[Cygwin](https://www.cygwin.com) is a Unix-Emulator that provides
a terminal in which Unix commands can be executed on Windows
using
[bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell))
(*Bourne Again Shell*) as command-line interpreter -
*bash* was developed in 1989 as a successor to the
*Bourne Shell:*
*[sh](https://en.wikipedia.org/wiki/Bourne_shell)*.
[<bash terminal>](https://cdn.ttgtmedia.com/rms/onlineimages/REF_bash_command_line_3.jpg)
*Cygwin* is <span style="text-decoration:underline">not</span>
a Unix container or virtual machine system (unlike
[WSL](https://learn.microsoft.com/en-us/windows/wsl/about)).
*Cygwin* emulates most (not all) Unix system calls such that
most Unix commands can execute on Windows.
Alternatives to *Cygwin* such as
[GitBash]()
can be used, but have some flaws and drawbacks on Windows
(*GitBash*, for example, performs strange path conversions, see
[link](https://stackoverflow.com/questions/54258996/git-bash-string-parameter-with-at-start-is-being-expanded-to-a-file-path)).
Good introductions to *bash* are:
- [https://cs.lmu.edu/~ray/notes/bash](https://cs.lmu.edu/~ray/notes/bash).
- Also recommended is the
[Tutorial for Beginners](https://linuxconfig.org/bash-scripting-tutorial-for-beginners).
&nbsp;
## Steps
1. [Installation](#1-installation)
2. [Configure paths in .bashrc](#2-configure-paths-in-bashrc)
&nbsp;
## 1. Installation
1. Download the installer `setup-x86_64.exe` from
[https://www.cygwin.com/install.html](https://www.cygwin.com/install.html).
1. Run the installer `setup-x86_64.exe`.
- select useful Unix commands during installation:
- Editors/`vim` - visual editor,
- Web/`wget` - web downloader,
- Net/`curl` - web downloader.
1. Change *cygwin* default path `/cygdrive/c` to: `/c`:
- navigate to the *cygwin* installation directory.
- inside, edit `/etc/fstab` and replace line
```
none /cygdrive cygdrive binary,posix=0,user 0 0
with:
none / cygdrive binary,posix=0,user 0 0
```
1. Add bash-user that works with Windows and create
*bash* HOME directory.
- Find out your Windows user name: <user_name>
- Create or select a directory that you want to use as
HOME-directory for *bash*, e.g. your Windows
HOME-directory `C:\Users\<user_name>`.
- Edit file `/etc/nsswitch.conf`:
- to use Windows HOME-directory, comment line
```
#db_home:
```
- for using a new directory as HOME-directory, enter
```
db_home: /cygdrive/c/<path>
```
1. Open a new *bash* terminal and test changes:
```sh
$ whoami
<user_name> # your user name appears
$ echo $HOME # print path to HOME-directory
<home_directory> # output, e.g. /c/Users/svgr
$ cd $HOME # change into HOME-directory
$ ls -la # show content
total 203
drwxr-xr-x+ 1 svgr2 Kein 0 Oct 4 22:20 .
drwxrwx---+ 1 svgr2 Kein 0 Jan 1 2022 ..
-rw------- 1 svgr2 Kein 14476 Oct 4 22:20 .bash_history
-rwxr-xr-x+ 1 svgr2 Kein 2717 Oct 4 20:28 .bashrc
...
$ echo Hello >hello.txt # create file hello.txt with content Hello
$ ls -la # show new file exists in $HOME
total 203
-rw-r--r-- 1 svgr2 Kein 6 Oct 7 23:38 hello.txt
...
$ cat hello.txt # output content of file
Hello
```
1. Open Windows file explorer (
[ ? ](https://geekflare.com/wp-content/uploads/2021/06/14-alternative-file-managers-to-replace-windows-10-file-explorer.jpg)
) and navigate to file `hello.txt`.
Open the file by clicking (ending `.txt` should open the
file with *notepad*).
1. Return to *bash* terminal:
- change to HOME-directory and
- open file `.bashrc`
```sh
$ cd # change to bash HOME-directory
$ ls -la # find file .bashrc
total 203
-rwxr-xr-x+ 1 svgr2 Kein 2717 Oct 4 20:28 .bashrc
$ cat .bashrc # output file .bashrc
...
```
&nbsp;
## 2. Configure paths in `.bashrc`
[PATH](https://en.wikipedia.org/wiki/PATH_(variable)) is an
environment variable on Unix-like operating systems that
specifys a set of directories where executable programs are
located. A *"command not found"* error occurs when PATH is
not properly configured.
1. Open `.bashrc` using an editor, e.g. *vim* and append PATH
configurations.
```sh
$ vim .bashrc # open .bashrc in vim editor
```
1. Append PATH configurations at the end of the file - only
those relevant on your system - and djust paths for your
system:
```sh
# add Windows system paths
export PATH=".:/usr/bin:/usr/local/bin"
export PATH="${PATH}:$(cygpath ${SYSTEMROOT}):$(cygpath ${SYSTEMROOT})/system32"
# add Java path
export JAVA_HOME="/c/Program Files/Java/jdk-21"
export PATH="${PATH}:${JAVA_HOME}/bin"
# add Python path
export PYTHON_HOME="/c/Users/svgr2/AppData/Local/Programs/Python/Python312"
export PATH="${PATH}:${PYTHON_HOME}"
export PATH="${PATH}:${PYTHON_HOME}/Scripts"
# add Docker path
export DOCKER_HOME="/c/Program Files/Docker/Docker"
export PATH="${PATH}:${DOCKER_HOME}/resources/bin"
...
```
1. Verify paths have been added to *PATH* variable:
```sh
$ source .bashrc # reload .bashrc to activate chances
$ echo $PATH # show PATH
.:/usr/bin:/usr/local/bin:/c/WINDOWS:/c/WINDOWS/system32:/c/Program ...
$ echo $PATH | tr ':' '\n' # pretty print PATH
.
/usr/bin
/usr/local/bin
/c/WINDOWS
/c/WINDOWS/system32
/c/Program Files/Java/jdk-21/bin
/c/opt/maven/bin
/c/Users/svgr2/AppData/Local/Programs/Python/Python312
/c/Users/svgr2/AppData/Local/Programs/Python/Python312/Scripts
/c/Program Files/Docker/Docker/resources/bin
/c/Program Files/MySQL/MySQL Workbench 8.0 CE
/c/Program Files (x86)/Git/bin
/c/Program Files/Oracle/VirtualBox
/c/opt/Qt6/Tools/mingw1120_64/bin
/c/opt/Qt6/6.2.4/mingw_64/bin
```
Paths may vary based on your system.
1. Start programs to verify paths have properly been set.
```sh
$ java --version
java 21 2023-09-19 LTS
Java(TM) SE Runtime Environment (build 21+35-LTS-2513)
Java HotSpot(TM) 64-Bit Server VM (build 21+35-LTS-2513, mixed mode, sharing)
$ python --version
Python 3.12.0
$ docker --version
Docker version 24.0.6, build ed223bc
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment