Python Environment for Mac

# Python environment on Mac

# Install pyenv and Python  
1. Install pyenv
    ```shell
    curl https://pyenv.run | bash
    ```
2. Config Environmental Variables
    Add the following to  ```.zshrc``` 
    ```shell
    export PYENV_ROOT="$HOME/.pyenv"
    export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init --path)"
    ```
3. Reflection of Config [^source]
    ```shell
    . ~/.zshrc
    ```
4. Install Python
    Run the following commands under an administrative directory like your home directory.

    Install required version of python by pyenv command:
    ```shell
    pyenv install 3.11.5
    ```
    If you need to check what versions can install via pyenv:
    ```shell
    pyenv install --list
    ```
    You can check installed python version following:
    ```shell
    pyenv versions
    ```
    If installtion stopped on the way, it may be due to the loaded module.
    Check dependencies by:
    ```shell
    module purge
    ```
5. Reflact Installed Python
    If set default python version:
    ```shell
    pyenv global 3.8.5
    ```
    If you want to set the python version only in a specific directory, move to that folder and use the local option.
    ```shell
    pyenv local 3.8.5
    ```
    You can check these version following commands and local config take precedence over global.
    ```shell
    pyenv global
    pyenv local
    ```

# Usage
1. Create Project-directory
    Create directory named sample1.
    ```shell
    mkdir sample1
    ```
2. Create Virtual Environment
    Run the following command, in the sample1 directory.
    ```shell
    python3 -m venv .venv
    ```
    This command creates a virtual environment at the specified directory named .venv.
    You can change the virtual environment name (in the case of this example, this is .venv) is arbitrary.
    If you organize a project by git, add the venv folder to .gitignore.
3. Switch to Virtual Environment
    Run bin/activate in the created virtual environment, .venv.
    ```shell
    . .venv/bin/activate
    ```
4. Install Packages
    For instance, to install the requests package, use:
    ```shell
    python3 -m pip install requests
    ```
    You can do this by: 
    ```shell
    pip3 install requests
    ```
5. End Virtual Environment
    ```shell
    deactivate
    ```
    Check the prompt was changed.
    
# Copy Virtual Environment
A list of packages can be created by following the command. 
Here, create a list in a file named requirements.txt.
```
python3 -m pip freeze > requirements.txt
```
When you create a new virtual environment or add some new packages, install packages to the virtual environment by the following command at once.
```shell
python3 -m pip install -r requirements.txt
```
When using multiple packages (like for main and develop), create a separated file such as requirements-dev.txt, add
```text
-r requirements.txt
``` 
and run
```shell
pip3 install -r requirements-dev.txt
```
This can also install packages in requirements.txt.
So, requirements-dev.txt is follows.
```text
-r requirements.txt
black==23.3.0
flake8==6.0.0
```

[^source]: ```.``` is equal to```source```.

# References
* [横田研](https://rioyokotalab.github.io/python-supercomputer/)
* [python Japan](https://www.python.jp/install/macos/virtualenv.html)
* [Shun Sakamoto](https://qiita.com/shun_sakamoto/items/7944d0ac4d30edf91fde)
* [Ryo-0131](https://qiita.com/Ryo-0131/items/321ae71ec15f969cd43f)