How do I add paths to my virtualenv so that both PyCharm and the interpreter can find the external packages?

  • 3
  • 9

Yesterday, I edited the `bin/activate` script of my virtualenv so that it sets the `PYTHONPATH` environment variable to include a development version of some external package. I had to do this because the setup.py of the package uses distutils and does not support the `develop` command à la setuptools. Setting `PYTHONPATH` works fine as far as using the Python interpreter in the terminal is concerned.

However, just now I opened the project settings in PyCharm and discovered that PyCharm is unaware of the external package in question - PyCharm lists neither the external package nor its path. Naturally, that's because PyCharm does not (and cannot reliably) parse or source the `bin/activate` script. I could manually add the path in the PyCharm project settings, but that means I have to repeat myself (once in `bin/activate`, and again in the PyCharm project settings). That's not DRY and that's bad.

Creating, in site-packages, a symlink that points to the external package is almost perfect. This way, at least the source editor of PyCharm can find the package and so does the Python interpreter in the terminal. However, somehow PyCharm still does not list the package in the project settings and I'm not sure if it's ok to leave it like that.

So how can I add the external package to my virtualenv/project in such a way that…

1. I don't have to repeat myself; and…
2. both the Python interpreter and PyCharm would be aware of it?
Answered by Dmitry Jemerov
 
Where exactly do you expect PyCharm to list the package? The list of packages in Settings | Python Interpreters shows only packages installed into your PyCharm installation using pip or setuptools; it won't show packages installed in any other way, and it shouldn't matter in your situation.
Where exactly do you expect PyCharm to list the package? The list of packages in Settings | Python Interpreters shows only packages installed into your PyCharm installation using pip or setuptools; it won't show packages installed in any other way, and it shouldn't matter in your situation.
--
Dmitry Jemerov, Chief Technology Officer, JetBrains

Exactly. I was expecting to see the package in Settings | Python Interpreters. Now I understand that only packages installed through pip or setuptools are shown there.

I have now removed the `PYTHONPATH` from `bin/activate` and, instead, use a symlink in `site-packages`. So it's safe to leave it like that (with my external package not showing up in Settings | Python Interpreters), right?

Yes, this should be OK.
--
Dmitry Jemerov, Chief Technology Officer, JetBrains