CLJ
CLJ

Reputation: 1927

Pycharm environment different than command line

I am having an issue getting my Pycharm environment to match up with the environment that I have on the command line. I recently removed python and reinstalled it via home brew. The python in my path is pointing to /usr/local/bin/python I added PATH=/usr/local/bin:$PATH to the beginning of my .bash_profile file and I can execute the following code just fine in the interperter on the command line. However, when I add /usr/local/bin/python to the project python interpreters and run the below code I get the attribute error. Can anyone shed some light on how I can get Pycharm to use the same environment as my command line?

import sqlite3
db = "mydb.db"
conn = sqlite3.connect(db)
conn.enable_load_extension(True)

AttributeError: 'sqlite3.Connection' object has no attribute 'enable_load_extension'

Upvotes: 25

Views: 28558

Answers (9)

BanikPyco
BanikPyco

Reputation: 819

I am an idiot and totally forgot I'm working in a virtual environment! The source /.profile command needs to be run on the activated virtual environment in Terminal! I think some may have a hard day (or similar brain dysfunction to mine) and find this helpful.

Upvotes: 0

TG Gowda
TG Gowda

Reputation: 11947

I am using pycharm on OSX. Pycharm > Preferences > Terminal had shell path as /bin/zsh. But this shell environment was not in sync with my other terminal emulators like iTerm. For example, my conda environment was not getting initialized (not even the base environment).

Solution: Changing the shell path from /bin/zsh to /usr/bin/env zsh fixed things for me. For bash, adding shell path as /usr/bin/env bash should work.

Upvotes: -1

Jason Wolosonovich
Jason Wolosonovich

Reputation: 504

I actually just found a solution for this that works in PyCharm 2017.1.2

uncheck Tools > Terminal > "shell integration"

source: answer from @Federicojama near the bottom of the page https://intellij-support.jetbrains.com/hc/en-us/community/posts/208567485-Pycharm-terminal-is-missing-part-of-PATH

Upvotes: 1

Ami Mahloof
Ami Mahloof

Reputation: 472

for me what worked was not running pycharm from the applications but from a terminal using chram . then it inherited all the env vars and paths

Upvotes: -1

ccray
ccray

Reputation: 101

Edit: clarifying that this workaround is for PyCharm 2016.3.0

It's really recommended that you just run the following command

source ~/.bash_profile

at the start of each terminal session until 2016.3.1 is released.

However, there is a workaround for this bug. The terminal script appears to have 2 function names reversed, so they must be renamed.

You have to edit the app's terminal plugin script to do this, which is NOT recommended.

On MacOSX, this is located here if PyCharm is installed globally (not sure where otherwise):

cd /Applications/PyCharm.app/Contents/plugins/terminal

Edit the 'jediterm-bash.in' file with the text processor of your choice. If should look like this:

#!/bin/bash

function load_login_configs {
  #       When bash is invoked as an interactive login shell, or as a  non-interac-
  #       tive  shell with the --login option, it first reads and executes commands
  #       from the file /etc/profile, if that  file  exists.   After  reading  that
  #       file,  it  looks  for  ~/.bash_profile, ~/.bash_login, and ~/.profile, in
  #       that order, and reads and executes  commands  from  the  first  one  that
  #       exists  and  is  readable.

  if [ -f /etc/profile ]; then
     source /etc/profile
  fi

  if [ -f ~/.bash_profile ]; then
     source ~/.bash_profile
  else
     if [ -f ~/.bash_login ]; then
        source ~/.bash_login
     else
        if [ -f ~/.profile ]; then
           source ~/.profile
        fi
     fi
  fi
}

function load_interactive_configs {
  if [ -f ~/.bashrc ]; then
       source ~/.bashrc
  fi
}

if [ `shopt -q login_shell` ]; then
  load_login_configs
fi

load_interactive_configs

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
bind '"\e\e[C":forward-word'
bind '"\e\e[D": backward-word'
bind '"\e\O[C":forward-word'
bind '"\e\O[D": backward-word'

function generate_command_executed_sequence() {
   printf '\e\7'
}

export -f generate_command_executed_sequence


#generate escape sequence after command is executed to notify jediterm emulator
trap "generate_command_executed_sequence" DEBUG

if [ -n "$JEDITERM_USER_RCFILE" ]
then
  source $JEDITERM_USER_RCFILE
fi

if [ -n "$JEDITERM_SOURCE" ]
then
  source $JEDITERM_SOURCE
fi

Rename the following functions:

load_login_configs => load_interactive_configs

load_interactive_configs => load_login_configs

The final script should be:

#!/bin/bash

function load_interactive_configs {
  #       When bash is invoked as an interactive login shell, or as a  non-interac-
  #       tive  shell with the --login option, it first reads and executes commands
  #       from the file /etc/profile, if that  file  exists.   After  reading  that
  #       file,  it  looks  for  ~/.bash_profile, ~/.bash_login, and ~/.profile, in
  #       that order, and reads and executes  commands  from  the  first  one  that
  #       exists  and  is  readable.

  if [ -f /etc/profile ]; then
     source /etc/profile
  fi

  if [ -f ~/.bash_profile ]; then
     source ~/.bash_profile
  else
     if [ -f ~/.bash_login ]; then
        source ~/.bash_login
     else
        if [ -f ~/.profile ]; then
           source ~/.profile
        fi
     fi
  fi
}

function load_login_configs {
  if [ -f ~/.bashrc ]; then
       source ~/.bashrc
  fi
}

if [ `shopt -q login_shell` ]; then
  load_login_configs
fi

load_interactive_configs

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
bind '"\e\e[C":forward-word'
bind '"\e\e[D": backward-word'
bind '"\e\O[C":forward-word'
bind '"\e\O[D": backward-word'

function generate_command_executed_sequence() {
   printf '\e\7'
}

export -f generate_command_executed_sequence


#generate escape sequence after command is executed to notify jediterm emulator
trap "generate_command_executed_sequence" DEBUG

if [ -n "$JEDITERM_USER_RCFILE" ]
then
  source $JEDITERM_USER_RCFILE
fi

if [ -n "$JEDITERM_SOURCE" ]
then
  source $JEDITERM_SOURCE
fi

Save and restart PyCharm and you should be good to go.

Upvotes: 2

cmlccie
cmlccie

Reputation: 104

If you are using PyCharm version 2016.3 and have noticed that your PyCharm Terminal is no longer providing the same default environment as your MacOs Terminal environment, it is a bug that should be fixed in 2016.3.1 - whenever it releases. In the mean time, the following is a workaround that should 'default' all of your PyCharm projects back a more more MacOS-Terminal like PyCharm-Terminal:

Create a ~/.bashrc file with the following contents: source /etc/bashrc source /etc/bashrc_Apple_Terminal source ~/.bash_profile

This should setup your PyCharm Terminal (interactive bash session) and make it similar to the MacOS Terminal (login bash session). Once JetBrains patches and releases 2016.3.1, I recommend deleting this ~/.bashrc file. Hopefully this will get us all back to normal.

Upvotes: 4

DmitryFilippov
DmitryFilippov

Reputation: 596

.bash_profile is being read by bash (your command line interpreter) only. However if you want to preserve bash environment for PyCharm there is one true Linux way.

Run PyCharm from your command line (from bash). Thus environment variables will be inherited from bash to pycharm. Read $man environ for information on linux environment inheritance process. So all you need is just launch ${PATH_TO_PYCHARM}/bin/pycharm.sh from command line. Or create launcher which invokes bash for PyCharm launching.

Thats it ! Hope that works for you.

Upvotes: 30

TinaW
TinaW

Reputation: 1017

Unfortunately in Mac OS X, graphic applications do not inherit your .bash_profile config. I'm posting the updated workaround for OSX 10.11 on how to set up environment variables at GUI level:

There is a useful repo called osx-env-sync which reads ~/.bash_profile and sets the environment variables exported in it for GUI applications. After copying 2 files and running 2 other commands described on the github page, Pycharm can be launched in quick-start with global variables available as defined in the bash_profile.

This link gives further background information.

Upvotes: 1

Temak
Temak

Reputation: 3009

Another approach is to source your script setting environmental variables (for example .bash_profile) by adding a line . /path/to/script into PY_CHARM_INSTALL_DIR/bin/pycharm.sh.

After that you can run pycharm using quick-lunch or whatever and you variables will be there.

Upvotes: 3

Related Questions