1
0

Python Environment for Mac

Last updated at Posted at 2024-03-09

Python environment on Mac

Install pyenv and Python

  1. Install pyenv

    curl https://pyenv.run | bash
    
  2. Config Environmental Variables
    Add the following to .zshrc

    export PYENV_ROOT="$HOME/.pyenv"
    export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init --path)"
    
  3. Reflection of Config 1

    . ~/.zshrc
    
  4. Install Python
    Run the following commands under an administrative directory like your home directory.

    Install required version of python by pyenv command:

    pyenv install 3.11.5
    

    If you need to check what versions can install via pyenv:

    pyenv install --list
    

    You can check installed python version following:

    pyenv versions
    

    If installtion stopped on the way, it may be due to the loaded module.
    Check dependencies by:

    module purge
    
  5. Reflact Installed Python
    If set default python version:

    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.

    pyenv local 3.8.5
    

    You can check these version following commands and local config take precedence over global.

    pyenv global
    pyenv local
    

Usage

  1. Create Project-directory
    Create directory named sample1.
    mkdir sample1
    
  2. Create Virtual Environment
    Run the following command, in the sample1 directory.
    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.
    . .venv/bin/activate
    
  4. Install Packages
    For instance, to install the requests package, use:
    python3 -m pip install requests
    
    You can do this by:
    pip3 install requests
    
  5. End Virtual Environment
    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.

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

-r requirements.txt

and run

pip3 install -r requirements-dev.txt

This can also install packages in requirements.txt.
So, requirements-dev.txt is follows.

-r requirements.txt
black==23.3.0
flake8==6.0.0

References

  1. . is equal tosource.

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0