Python set up on Mac

Python
Author

Tony Duan

Published

October 1, 2023

1 download python

https://www.python.org/downloads/macos/

2 check installed python version

2.1 in python code

Code
import sys
print(sys.version)
3.11.4 (v3.11.4:d2340ef257, Jun  6 2023, 19:15:51) [Clang 13.0.0 (clang-1300.0.29.30)]

2.2 in r code

Code
reticulate::py_config()
python:         /Library/Frameworks/Python.framework/Versions/3.11/bin/python3
libpython:      /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/config-3.11-darwin/libpython3.11.dylib
pythonhome:     /Library/Frameworks/Python.framework/Versions/3.11:/Library/Frameworks/Python.framework/Versions/3.11
version:        3.11.4 (v3.11.4:d2340ef257, Jun  6 2023, 19:15:51) [Clang 13.0.0 (clang-1300.0.29.30)]
numpy:          /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/numpy
numpy_version:  1.26.0

NOTE: Python version was forced by RETICULATE_PYTHON
Code
reticulate::repl_python()
exit

it may using version 3.9.18 under miniconda

2.3 in terminal

Code
jinchaoduan@Jinchaos-MacBook-Air ~ % python3
Python 3.11.4 (v3.11.4:d2340ef257, Jun  6 2023, 19:15:51) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

but in terminal,system is using python 3.11.4

Then we find the python path in terminal

Code
jinchaoduan@Jinchaos-MacBook-Air ~ % which python3
/Library/Frameworks/Python.framework/Versions/3.11/bin/python3
>>> 

3 config which python we use in rstudio

create .Renviron file containing the line RETICULATE_PYTHON=“xxxx/python3” in home directory

Code
touch $HOME/.Renviron
Code
open $HOME/.Renviron

put RETICULATE_PYTHON=“/Library/Frameworks/Python.framework/Versions/3.11/bin/python3”

3.1 in mac terminal

Code
python3 --version

4 install pip

What is pip? pip — or pip3, as it’s also known — is a package management system for Python that is used to install and manage software packages. It’s the tool recommended by the Python Software Foundation for installing Python applications. It connects to an online repository of public packages called Python Package Index. pip is installed by default with Python 3.4 and later.if not then run in terminal

Code
python -m ensurepip --upgrade

5 install python package

5.1 install python package in mac terminal

install jupyter in terminal

Code
python3 -m pip install seaborn

python3 -m pip install opendatasets

5.2 install python package in r code

install pandas in r

Code
library(reticulate)
py_install("pandas")

6 check python package version

check pandas version

Code
import pandas
print(pandas.__version__)
2.0.3

list all installed pacakge in terminal

Code
pip list

7 update python package

7.1 in terminal

update numpy

Code
python3 -m pip install --upgrade numpy

8 uninstall package

uninstall numpy

Code
python3 -m pip uninstall numpy

9 install jupyter on mac

Code
pip3 install jupyter

open jupyter

Code
jupyter notebook

check jupyter

Code
which jupyter

10 install jupyter lab on mac

Code
pip3 install jupyterlab

open jupyter lab

Code
jupyter lab

11 jupyter notebook short cut

exit edit mode

Code
esc

enter edit mode

Code
enter

Change cell to code

Code
y

Change cell to markdown

Code
m

Insert cell above

Code
a

Insert cell below

Code
b

delete cell

Code
d+d

Undo last action

Code
Command + Z 

Redo last action

Code
Command + Y

12 get current working directory

Code
import os
# get the current working directory
current_working_directory = os.getcwd()
print(current_working_directory)
/Users/jinchaoduan/Documents/GitHub/tidystep/posts/Python set up on Mac

13 change working directory

Code
import os

# change directory
os.chdir('/Users/jinchaoduan/Documents/GitHub')

print(os.getcwd())
/Users/jinchaoduan/Documents/GitHub

14 read csv

Code
import sys
import pandas as pd
Code
df = pd.read_csv('MrBeast_youtube_stats.csv')
Code
df.head()
            id  ... contentDetails.contentRating.ytRating
0  TQHEJj68Jew  ...                                   NaN
1  00NgUctWoLQ  ...                                   NaN
2          NaN  ...                                   NaN
3  ayXxwJJId_c  ...                                   NaN
4  cExLQ1o2pDw  ...                                   NaN

[5 rows x 26 columns]

15 Reference

https://noteable.io/blog/jupyter-notebook-shortcuts-boost-productivity/

https://stackoverflow.com/questions/50145643/unable-to-change-python-path-in-reticulate/58743111#58743111