2

I'm trying to follow this SO answer and this blog post to activate my virtualenv automatically in PyCharm's Terminal (no need for source path, etc..). The difference in my case is that my virtualenv folder resides in my project's root folder, and that's where I want the shell --rcfile to be as well.

(Note: this isn't about how to integrate a virtualenv in project, I've already done that)

After many retries, I got it to work, however with a quibble that I'm not sure that I should live with. Here are my configurations:

  • My virtualenv lives in a /venv/ directory at the root folder of my project (called project1).

  • My shell rcfile (at user/learnp/project1) override file looks like this:

    source ~/.bashrc
    source ~/learnp/project1/venv/bin/activate
    
  • In PyCharm Preferences → Tools → Terminal, my Shell Path adjustment looks like this:

    /bin/bash --rcfile ~/learnp/project1/h.pycharm-bash 
    

[previously: /bin/bash]

  • When I launch my Terminal in PyCharm, this is what I see:

    bash: /Users/user/.bashrc: No such file or directory
    (venv)bash-3.2$ 
    

So venv is activated, however two things I find worrying is that it's saying there isn't a file name bashrc, and that the prompt is saying bash-3.2 instead of showing the regular Terminal prompt with my username in it.

How can I fix this? what did I do wrong?

Community
  • 1
  • 1
zerohedge
  • 3,185
  • 4
  • 28
  • 63

1 Answers1

0

According to the blog post that you linked, the .pycharm-bash has the line:

source ~/.bashrc

This instructs your shell execute the /User/user/.bashrc file within it's current context. The problem for you is that this file doesn't exist.

The best way to deal with this problem is by adding these lines to .pycharm-bash:

if [ -e ~/.bashrc ]
then
    source ~/.bashrc
fi

These lines mean 'source the file /User/<current user>/.bashrc if it exists, otherwise do nothing.'

I would suggest that you take a look at what bashrc does, as it is an important part of programming in the Unix environment. This answer can shed some light on what the file does.

Community
  • 1
  • 1
Bobby Russell
  • 475
  • 2
  • 12
  • Thanks Bobby. I'm pretty sure I don't have a `~/.bashrc` file, however I'm not sure I should just remove this line. Shouldn't I strive to get a prompt that is as similar to the usual terminal one (and PyCharm's usual one)? What can I do to try and locate the system version of this file? – zerohedge Nov 16 '15 at 16:27
  • 1
    You can totally just remove this line if you don't have a bashrc file. The line means 'execute the shell commands inside of the file ~/.bashrc.' If you don't have a bashrc file, then what is there to execute? At any rate, I've updated my answer to include a condition that checks for the file, and sources it if it exists. – Bobby Russell Nov 16 '15 at 16:33
  • The normal PyCharm terminal prompt looks like this: `Users-Macbook-Pro:project1 user$ [blinking cursor]` while mine currently looks like this: `(venv) bash3.2$`. I think it's more comfortable to work knowing which directory you are in. I don't know if there are practical ramifications beyond that, but surely there is if people were referencing that file in **both** answers? – zerohedge Nov 16 '15 at 16:37
  • @zerohedge, if you don't have a `~/.bashrc` file, the `source ~/.bashrc` file command will always fail. It's as simple as that. If you don't have a bashrc file, then leaving out the `source ~/.bashrc` will not change anything for you -- there is no bashrc file to be sourced. the `bash3.2` prompt that you see in your term is a naked bash prompt. You need to add shell commands to show things like your pwd in the terminal. Here is an example bashrc that does something similar: http://tldp.org/LDP/abs/html/sample-bashrc.html. Again, read the documentation on what bashrc does. – Bobby Russell Nov 16 '15 at 16:43
  • Yes, thank you. But that file seems critical, maybe it's in another folder in my case, so I just need to find it. I guess this for another question. – zerohedge Nov 16 '15 at 16:45
  • It is not another file. The system looks for the `~/.bashrc` file when you load the shell. If it is not there, it is not being sourced. This is what I am trying to tell you. It is not a critical file, meaning you don't need this file to exist for you to start your shell, so you don't need one. It generally is used to source environment variables into your environment, and to change your shell's appearance. PyCharm sources another file that does that, but it is clearly not your `~/.bashrc`. The only notable thing about the ~/.bashrc is that it is auto loaded when you start your shell. – Bobby Russell Nov 16 '15 at 16:52
  • You can also simply create an empty `.bashrc` file in your home directory. – chepner Nov 16 '15 at 17:21