0

My PyCharm zsh shell does not have full user PATH environment variables.

Currently, in PyCharm preferences, my shell path is /bin/zsh.

Custom config is set in ~/.zshenv
ZDOTDIR=$HOME/zshconfig

It does not have any of the additional $PATH or aliases that I have added in my ~/zshconfig/.zprofile or ~/zshconfig/.zshrc.

Also, trying to run zsh as an interactive login shell from PyCharm also does not work. (setting PyCharm shell path to: /bin/zsh --interactive --login does not work)

How do I run a normal terminal environment in PyCharm?

Darin
  • 25
  • 7

3 Answers3

0

This answer should apply to sh, bash, zsh, and fish shells

The issue is that PyCharm has this option called "Shell integration".
To quote their website on the "Shell integration" option:

Integrate the terminal with the system shell to properly keep track of your command history for sessions and load a custom config file with required environment variables.

Shell integration works for sh, bash, zsh, and fish shells.
source

What this does is create a custom shell environment (managed by PyCharm) that does NOT source your ~/.zprofile or ~/.zshrc.
(FYI: ~/.zshenv is sourced always)

There are 3 options to add your environment variables.

  1. Disable the "Shell integration" option. This will launch a normal shell (--interactive --login) that PyCharm does not manage. This shell will be your normal user configurated shell.

  2. Add your environment variables to PyCharm.
    In PyCharm terminal settings, you can add PATH: <directory you want to add to path>:$PATH as an Environment Variable. (This may be required for each project)

  3. (Not Recommended) Export your $PATH variables in .zshenv.
    Add export PATH="<directory you want to add to path>:$PATH" to your .zshenv file.
    This will be applied on any shell that runs! Not just login and interactive shells.
    zsh config files reference
    zsh config files

Darin
  • 25
  • 7
  • I never worked with PyCharm, but it seems that you can [set environment variables in it](https://stackoverflow.com/questions/42708389/how-to-set-environment-variables-in-pycharm). If you define there a variable `PyCharm=YES`, you can then query this variable in your zsh startup files and apply certain settings only if run from a PyCharm terminal. – user1934428 Sep 03 '20 at 11:09
  • These files are never sourced by PyCharm itself, but by the zsh it starts, and of course can pass parameters to this Zsh, to influence the startup behaviour to a certain extent. Since you say that the `.zshenv` is processed, we can at lesat conclude that PyCharm did at least not fiddle with the ZDOTDIR variable, and since the resulting shell is interactive, `.zshrc` is sourced too. If you really think it won't be sourced (which looks like a bug to me), you can always source it manually. – user1934428 Sep 04 '20 at 12:32
  • @user1934428 That's an interesting idea. Set it in PyCharm then check for `PYTCHARM` in `. zshenv`. It it's true, source your `.zprofile` and `zshrc` manually? – Darin Sep 04 '20 at 12:37
  • @user1934428 I tested it on my Mac and `.zshrc` was not sourced by default. Maybe this is just a bug – Darin Sep 04 '20 at 12:40
  • Only when run from within PyCharm? This would be odd. Otherwise, I am using zsh on the Mac too, and it works like advertised. How did you verify that your .zshrc was not processed? – user1934428 Sep 04 '20 at 12:43
  • @user1934428 Yeah, only in PyCharm. I added a path with a command and also added `echo "interactive shell"` to `.zshrc`. Neither worked when "Shell integration" is turned on. They work in a normal terminal though – Darin Sep 04 '20 at 13:40
  • I do have a custom zsh structure with the config files in a separate folder though. Maybe that doesn't work with PyCharm – Darin Sep 04 '20 at 13:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220980/discussion-between-darin-and-user1934428). – Darin Sep 04 '20 at 14:16
  • That made me try something... I moved my configs from `~/zshconfig` to `~/`. Then they worked fine! For some reason, PyCharm appears to be reading `~/.zshrc` and `~/.zprofile` directly from the home directory even though `ZDOTDIR` is set. In my `~/.zshenv` I have `ZDOTDIR=$HOME/zshconfig`. This works fine for normal terminals but apparently not for PyCharm... oof – Darin Sep 04 '20 at 14:28
  • I just fixed it another way... I made a `~/.zshrc` in the home directory and sourced my file in the folder: ```# PyCharm reads this file directly, doesn't follow "ZDOTDIR=$HOME/zshconfig" echo "interactive shell (~/.zshrc)" # If this file gets read directly: source ~/zshconfig/.zshrc``` And did the same for `.zprofile` Now it works fine even with "Shell integration" turned on! – Darin Sep 04 '20 at 14:35
  • Think about this from the viewpoint of zsh. If you read the zsh man-page, it is described clearly what it is doing when starting up, and what factors can have influence. Also, I wouldn't trace the startup process by placing `echo` commands here and there, but by getting a full trace via `set -x`. – user1934428 Sep 05 '20 at 14:37
  • @user1934428 Thanks! That's good to know. I'll try using `set - x` next time – Darin Sep 06 '20 at 15:19
0

My First Answer was a workaround until I realized the actual issue was that PyCharm was not following ZDOTDIR=$HOME/zshconfig. My original solution still works but this appears to be the actual issue.

If the "Shell integration" option is turned on, PyCharm does not follow $ZDOTDIR!
That means if you have your .zshrc in a custom folder (Ex. ~/zshconfig/.zshrc, PyCharm will not read it. PyCharm will just look in the default location, which is ~/.zshrc.

I would be curious if anyone has a way to have PyCharm follow your $ZDOTDIR!

My better solution, for the time being, is to create a file in my home directory and source my custom file location.

Create the file that PyCharm reads directly ~/.zshrc:

# For debugging which file gets sourced
#echo "interactive shell (~/.zshrc)"

# PyCharm reads this file directly, doesn't follow "ZDOTDIR=$HOME/zshconfig"
# If this file gets read directly, source my custom location:
source ~/zshconfig/.zshrc

And respectively for ~/.zprofile

Darin
  • 25
  • 7
0

Well, here is my third answer...

PyCharm does not follow ZDOTDIR=$HOME/zshconfig when set in your ~/.zshenv

Unfortunately, PyCharm does not support the “FOO=bar /bin/zsh” notation, so we need a wrapper script to launch it. source

What we want to do is make PyCharm use our custom $ZDOTDIR directory. Add a file in your home directory ~/pycharm_zsh.sh: Change the '~/zshconfig' to the correct path for your configurations

#!/bin/sh
export ZDOTDIR=~/zshconfig
/bin/zsh --login --interactive

Make that script executable: chmod +x ~/pycharm_zsh.sh

In pycharm, set "Shell path" to your custom script: /Users/<username>/pycharm_zsh.sh

Darin
  • 25
  • 7
  • The wrapper can be written slightly shorter as `ZDOTDIR=~/zshconfig /bin/zsh --login --interactive`. Since `/bin` is likely in the PATH anyway, you don't need to provide an absolute path to zsh. Since the zsh is connected to a tty, I guess that you don't need `--interactive`, as this should be the default; but this is certainly something you better test well. – user1934428 Sep 07 '20 at 10:18