Importing a Python module works from command line, but not from PyCharm

65,561

Solution 1

As ByteCommander said in a comment, PyCharm doesn't use bashrc, so it doesn't know where your library is.

In the same screen where you added the interpreter you can see a wheel icon, click it, it will show you a menu, click on more. You should see a screen like this:

PyCharm Interpreter configuration

You should select your interpreter and click on the last button. This should open this window:

Interpreter paths configuration

Now clicking on the plus icon you should be able to add your own paths for libraries.

Solution 2

Programs started from the Ubuntu launcher do not read .bashrc. As an alternative to setting the paths in PyCharm, you can simply start PyCharm from a Bash shell to give it access to the environment variables you set in .bashrc.

Share:
65,561

Related videos on Youtube

Karnivaurus
Author by

Karnivaurus

Updated on September 18, 2022

Comments

  • Karnivaurus
    Karnivaurus over 1 year

    My default Python binary is set to the one with the Anaconda distribution of Python. This is found at /home/karnivaurus/anaconda/bin/python, and I have made this the default by adding to my .bashrc file the following: export PATH=/home/karnivaurus/anaconda/bin:$PATH.

    I also have a Python package called caffe, which is located at /home/karnivaurus/caffe/distribute/python, and I have added this to the package search path by adding to my .bashrc file the following: export PYTHONPATH=${PYTHONPATH}:/home/karnivaurus/caffe/distribute/python.

    Now, I have a simple Python file, called test.py, with the following contents:

    import caffe
    print "Done."
    

    If I run this by entering python test.py into the terminal, it runs fine, printing out "Done.". The problem I am having is when I run this in the PyCharm IDE. In PyCharm, I have set the interpreter to be /home/karnivaurus/anaconda/bin/python. But when I open test.py in PyCharm, and run the file in the IDE, I get the following error:

    ImportError: No module named caffe
    

    So my question is: Why can PyCharm not find the caffe module when it runs the Python script, but it can be found when I run the script from the terminal?

    Thank you!

    • Byte Commander
      Byte Commander over 8 years
      I guess PyCharm does not care about your .bashrc, as that's a bash-specific config file. You have to set that variable either in PyCharm or in e.g. .profile
  • Javier Rivera
    Javier Rivera over 8 years
    And this is a good example about why giving instructions is easier with command line than GUIs.
  • Karnivaurus
    Karnivaurus over 8 years
    Thanks. Is this the way to add paths for both binary libraries, and Python modules? Or should Python modules be added to the "Source Root"?
  • Javier Rivera
    Javier Rivera over 8 years
    Both. You should only use Source Root if you are going to make changes to that modules (you don't need to add then to Source Root for completion, documentation or 'going to source' to work).
  • User007
    User007 over 6 years
    Is there a way to make programs read .bash_rc file when started from launcher?