PyCharm Command Not Found When Using Virtualenv & MacPorts

I recently switched a project over to using virtualenv for my PyCharm Django server run/debug configuration and ran into a ‘command not found’ error when trying to use another MacPorts installed package (sass) from Django (via Django Compressor).

Here is my original server configuration in PyCharm:

A quick look at the PATH environment variable identified the trouble:

MacPorts Python (/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7)

>>> print os.environ['PATH']
/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

Virtualenv Python (/Users/chriskief/.virtualenvs/exampleproject/bin/python)

>>> print os.environ['PATH']
/usr/bin:/bin:/usr/sbin:/sbin:/Users/chriskief/.virtualenvs/exampleproject/bin

The MacPorts bin and sbin are missing from the PATH, and that’s where sass lives:

which sass
/opt/local/bin/sass

The solution is to add the MacPorts directories (/opt/local/bin:/opt/local/sbin:$PATH) to your environment variables’ PATH in the run/debug configuration:

As a side note, if you run the Django server using the virtualenv from your terminal, you won’t encounter this error because MacPorts includes these directories in your .profile PATH:

# MacPorts Installer addition on 2013-10-26_at_16:30:30: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.

Troubles with PyCharm 3

PyCharm 3 was released last week and it looks like they have changed the way settings are interpreted for Django (I really should have tested their EAP releases, next time…). Unfortunately this means that if you implement your settings Disqus style, the IDE can no longer figure things out. This results in invalid template errors and missing manage.py commands when hitting option + r. I’ve logged the issue for JetBrains so we’ll see if they consider this a bug or not.

IDEs for Python and Django

As mentioned in the previous post, I’m a big fan of the IDE offerings from JetBrains (and who doesn’t love Prague). Tons of functionality, lightweight (when compared to my old friend Eclipse), lots of plugins and inexpensive.

One trick for any MacPorts users out there (yes, MacPorts, don’t judge), if you launch the application normally, you’ll end up using the system Python:

$ which python
/usr/bin/python

No bueno. To fix this little inconvenience, PyCharm and PhpStorm offer the ability to create a command-line launcher:

You can then launch the IDE via the command-line and you’ll now be using the MacPorts binaries:

$ which python
/opt/local/bin/python