Python environment on Mac
Install pyenv and Python
-
Install pyenv
curl https://pyenv.run | bash
-
Config Environmental Variables
Add the following to.zshrc
export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init --path)"
-
Reflection of Config 1
. ~/.zshrc
-
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
-
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
- Create Project-directory
Create directory named sample1.mkdir sample1
- Create Virtual Environment
Run the following command, in the sample1 directory.This command creates a virtual environment at the specified directory named .venv.python3 -m venv .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. - Switch to Virtual Environment
Run bin/activate in the created virtual environment, .venv.. .venv/bin/activate
- Install Packages
For instance, to install the requests package, use:You can do this by:python3 -m pip install requests
pip3 install requests
- End Virtual Environment
Check the prompt was changed.
deactivate
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
-
.
is equal tosource
. ↩