Setting paths in .zprofile or .zshrc?

Hi,


With the risk of asking really stupid questions, but DuckDuckGo/Google were at least confusing.

Anyway, when installing Python 3.8 I noticed that the path was set in .zprofile ( in my home folder) automatically. I amended it such that the path to Python was appended to the other paths, as to give the default paths preference:


path+=('path-to-python3')

echo PATH


However, I noticed that it was still preceding another path that I appended earlier in the .zshrc file. Just for my understanding: am I correct in assuming that .zprofile has preference over .zshrc? Also, wouldn't it be wiser to add all paths to .zprofile and delete .zshrc, or is that not so smart? Many thanks in advance, gurus!


Regards,

Bas

Posted on May 24, 2020 7:51 AM

Reply
Question marked as Top-ranking reply

Posted on May 24, 2020 8:11 AM

It is more complicated that just a "preference". Here is the relevant section from the zsh man page:


STARTUP/SHUTDOWN FILES
Commands are first read from /etc/zshenv; this cannot be overridden. Subsequent behaviour is modified by the
RCS and GLOBAL_RCS options; the former affects all startup files, while the second only affects global
startup files (those shown here with an path starting with a /). If one of the options is unset at any
point, any subsequent startup file(s) of the corresponding type will not be read. It is also possible for a
file in $ZDOTDIR to re-enable GLOBAL_RCS. Both RCS and GLOBAL_RCS are set by default.

Commands are then read from $ZDOTDIR/.zshenv. If the shell is a login shell, commands are read from
/etc/zprofile and then $ZDOTDIR/.zprofile. Then, if the shell is interactive, commands are read from
/etc/zshrc and then $ZDOTDIR/.zshrc. Finally, if the shell is a login shell, /etc/zlogin and $ZDOTDIR/.zlo-
gin are read.

The zsh user's guide describes interactive and login shells.


In your case, this is likely both an interactive and a login shell. Therefore, both .zprofile and .zshrc are being read.

2 replies
Question marked as Top-ranking reply

May 24, 2020 8:11 AM in response to benyochanan

It is more complicated that just a "preference". Here is the relevant section from the zsh man page:


STARTUP/SHUTDOWN FILES
Commands are first read from /etc/zshenv; this cannot be overridden. Subsequent behaviour is modified by the
RCS and GLOBAL_RCS options; the former affects all startup files, while the second only affects global
startup files (those shown here with an path starting with a /). If one of the options is unset at any
point, any subsequent startup file(s) of the corresponding type will not be read. It is also possible for a
file in $ZDOTDIR to re-enable GLOBAL_RCS. Both RCS and GLOBAL_RCS are set by default.

Commands are then read from $ZDOTDIR/.zshenv. If the shell is a login shell, commands are read from
/etc/zprofile and then $ZDOTDIR/.zprofile. Then, if the shell is interactive, commands are read from
/etc/zshrc and then $ZDOTDIR/.zshrc. Finally, if the shell is a login shell, /etc/zlogin and $ZDOTDIR/.zlo-
gin are read.

The zsh user's guide describes interactive and login shells.


In your case, this is likely both an interactive and a login shell. Therefore, both .zprofile and .zshrc are being read.

May 24, 2020 1:17 PM in response to benyochanan

If you install Xcode 11, or the corresponding Command Line tools for Xcode 11, Apple will install a subset of Python3 into Catalina. I happen to only install Python 3 (3.8.3) from Python.org, and want nothing to do with the Apple Python3 in /usr/bin/python3. So I ensure that the Python.org path remains just as it was set during that installation.


Here is my ~/.zprofile. content. Note how I set my preferred PATH first, and then allow the Python installer to subsequently prepend their library path, so the related Python.org binaries can be found in /usr/local/bin, when I run for instance, #!/usr/bin/env python3 in a script.


export PATH="$PWD:$HOME/bin:/usr/local/bin:${PATH}"
autoload -U colors && colors
MyHost="$(/usr/sbin/networksetup -getcomputername)"
PS1="%{$fg[blue]%}${MyHost}: %~ %{$reset_color%} %% %{$fg[white]%}"
export EDITOR="/usr/bin/vim"
export HOMEBREW_NO_ANALYTICS=1
export MAXOSX_DEPLOYMENT_TARGET=$(sw_vers -productVersion)
export TK_SILENCE_DEPRECATION=1

# Setting PATH for Python 3.8
# The original version is saved in .zprofile.pysave
export PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
export MANPATH="/usr/share/man:/usr/local/share/man"

alias breakpath='sep=:;print -l ${(ps.$sep.)PATH}'

export CLICOLOR=1
export LSCOLORS=cxexhxbxdxegedabagacad

and my Zsh prompt:

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Setting paths in .zprofile or .zshrc?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.