virtualenv support?

Hi,

I've been doing Java development for the last few years and have absolutely fallen in love with IDEA.  I hate coding in any other IDE now.

However, I'm starting work on a new python project... django, to be exact.  One of the first things I'm trying to accomplish in IDEA is to figure out how to add a new Python SDK that points to a virtual python environment that I've set up using "virtualenv".  It seems like no matter which folder in my virtualenv heirarchy I attempt to select, IDEA tells me that it's not a valid Python SDK folder.

As a side effect, IDEA doesn't seem to recognize any of the python modules I've installed in my virtualenv.  I've tried to work around this by adding the module folders as dependencies in the "Module Settings" dialog, but that's not working either.

Out of curiosity I downloaded and fired up Netbeans to see how it handled the same tasks, and am sad to say that it was really easy to get it set up with my virtualenv and starting running things right away... plus it has an integrated debugger...

I'd much rather use IDEA... does anyone know if there is a way to work around this sort of thing?

4 comments

Well, I never was able to figure out how to get IDEA to recognize my virtualenv as a valid python SDK, nor get it to use the python binary from that virtual environment as the interpreter when running scripts.  However, I was able to come up with a sort of workaround:

To get the editor window to correctly recognize modules that were installed in my virtualenv, I did the following:

1) create a new Python SDK (Project Structure->Platform Settings->SDKS) and point it to the "main" python install on my machine, but give it a name that reflects that it is for use with the particular virtualenv for my current application that I am working on.

2) Still in the Project Structure window, having selected my newly created python SDK, add "<path-to-virtualenv>/lib/python2.6/site-packages" to the list of paths in BOTH the classpath and the sourcepath tabs.  It would be best to put these paths at the TOP of the list of class / source paths so any modules installed in your virtualenv would take precedence over modules in your system python libraries (in case you have different versions of a module installed in both places), but there doesn't appear to be any easy way to reorder those paths in the UI.

That got the editor window to stop complaining about unknown modules, and I could then ctrl-click on classes and methods and IDEA will take me to the correct place in the source code in my virtualenv.

However, I was still having a problem when I tried to run a script from IDEA because it was calling the system python without any reference to my virtualenv libraries.  To work around this, I found some documentation on the virtualenv page that contains a trick / hack to switch your python env at runtime.  I needed to add the following lines at the top of my code:

activate_this = '<path-to-my-virtualenv>/bin/activate_this.py'

execfile(activate_this, dict(__file__=activate_this))

After that IDEA was able to run the scripts successfully.

It's still far from ideal, because if you have more than one script that can be an entry point into your application, you will need to add those lines to each script.  Also, without a debugger, running the code through IDEA doesn't really offer much benefit over running it from the command line.  And in the case of django it seems to actually be *less* useful than running from the command line, because the development web server needs to be sent an interrupt signal (CTRL-C) in order to shut down, and IDEA doesn't seem to do that when you "stop" your run... so the server just stays alive in the background and you have to kill it from a shell anyway.

just thought I'd share my workarounds for the virtualenv.  If anyone has found a cleaner solution I would love to hear about it.  Thanks to all of the IntelliJ devs for all of the work put in so far... really looking forward to tighter integration with django in the future!

0

Is no virtualenv support planned for PyCharm? This comes up for us because PyCharm doesn't detect any Django support in the default Python environment... because we exclusively use virtualenv to support Django.

0

Hello adam,

It is definitely planned, but it's not implemented yet.

Is no virtualenv support planned for PyCharm? This comes up for us

because PyCharm doesn't detect any Django support in the default

Python environment... because we exclusively use virtualenv to support

Django.

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

0

Same with me.

Intellij does not pick up the virtualenv specific site-packages directory (same as cprice explained):

But I can fix editor-highlighting by adding it manually.

I also need the to use activate_this header trick from cprice to run python scripts in intellij.

So the issue survived a solid 6 years! I'm using 2016.2.4 Ultimate Edition on Mac. 

Not sure if it matters, but within my virtuenv the versions are

[brandl]$ pip --version

pip 8.1.2 from /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages (python 3.4)

[brandl]$ python --version

Python 3.4.3

Or is there an existing ticket I could vote for?

0

Please sign in to leave a comment.