PyCharm doesn't use selected virtualenv - django

PyCharm seems to ignore the configured virtualenv,
and use the base interpreter instead.
In my project at /Users/janos/dev/git/github/bashoneliners I have a virtualenv subdirectory, strictly with my project's dependencies installed in it:
$ . virtualenv/bin/activate
(virtualenv)janos at kronos in ~/dev/git/github/bashoneliners on master
$ pip -V
pip 1.5.6 from /Users/janos/dev/git/github/bashoneliners/virtualenv/lib/python3.4/site-packages (python 3.4)
(virtualenv)janos at kronos in ~/dev/git/github/bashoneliners on master
$ pip freeze
Django==1.9
Markdown==2.6.5
PyJWT==1.4.0
defusedxml==0.4.1
oauthlib==1.0.3
pep8==1.6.2
pyflakes==1.0.0
python-social-auth==0.2.13
python3-openid==3.0.9
requests==2.9.1
requests-oauthlib==0.6.0
six==1.10.0
tweepy==3.5.0
But if I add this virtualenv as Project Interpreter in PyCharm,
it shows completely different packages:
These packages are the same as in my system's base interpreter /opt/local/bin/python. This drives me nuts, I really need to use the packages from the virtualenv, not from my system.
This is with PyCharm Community Edition 5.0.3.
I didn't have this problem before with older versions of PyCharm.
I tried creating a completely new virtualenv,
both on the command line and using PyCharm,
and invalidating caches and restarting, but nothing seems to work.
PyCharm always shows the same list of packages,
and the packages of the virtualenv.
Even if I create an empty virtualenv within PyCharm,
it doesn't start empty, but filled with the same list of packages.
My project works perfectly when I run things on the command line,
such as running Django management commands, unit tests, everything.
I only have problems in PyCharm.
If I try to install packages, for example Django,
using PyCharm,
I get this error:
Of course permission denied on /opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages, that's the system interpreter.
It should be trying to install the package there,
but in /Users/janos/dev/git/github/bashoneliners/virtualenv.
Clearly it's not using pip from the virtualenv, but from the system.
I need to make to use the one from the virtualenv.

This is logged as a bug in the issue tracking system of JetBrains,
so hopefully it will get sorted out soon.
https://youtrack.jetbrains.com/issue/PY-18074
A possible workaround is to fall back to a previous version of PyCharm:
https://confluence.jetbrains.com/display/PYH/Previous+PyCharm+Releases
As of 2016 Jan 6, virtualenv works fine for me in PyCharm 4.5.4.
Some of the virtualenv previously registered using PyCharm 5.0.3 appear invalid, but that's fine. I actually deleted all registered interpreters and re-added only the virtualenv I needed.
An odd thing with this older version is that sometimes PyCharm shows the incorrect Python version (2.7 instead of 3.5), but it shows the correct list of modules as per the virtualenv, and the editor doesn't show build errors, so the Python version mixup doesn't seem to cause problems (just a bit scary).

Related

Continuing a Project using Pycharm instead of Visual Studio

I am currently in the middle of a Django project and considering switching from Visual Studio Code to Pycharm but after activating the Virtual Env it is not showing any Project Interpreter.
Also when I type in the terminal python manage.py runserver it is saying bash: python: command not found
Is there a way to continue the project using pycharm or do I have to install everything from start?
Sure you can proceed using any other IDE, in your case PyCharm.
Make sure that you define the Python variable in your PyCharm Settings so everything can be interpreted accordingly and to ensure that all required packages are accessible.
Go to File/Settings/project:Foo in your PyCharm IDE and then check for the Interpreter to be set up.
Additionally you might consider to scan/run your requirements.txt once again. Just to be sure.
if your using linux then install django to your project, pip install django

Changing default Python from 2.7.10 to 3.5.2 in terminal so I can install pip

I was hoping for some help in setting up a Python development environment on a Mac.
Background: I'm running a newly upgraded macOS Sierra 10.12.1, and setting up various parts of Python development on it. This macOS version already came with Python 2.7.10 installed - and I'd prefer to leave that alone for now.
So I installed Homebrew, and then used that in turn to install the latest python3, that is, Python 3.5.2; but I guess it's not the default for Terminal yet, since when I run the python -V command, I get this in the Terminal window:
Python 2.7.10
So now I've got at least 2 version of Python on my Mac, and that's fine I guess, but the latest Python one is not the one that is the 'default'. How do I set 3.5.2 as my default rather than 2.7.10?
(Backstory for why I want to do that... pip is not installed, i.e., when I go to Terminal and type in pip, it says:
-bash: pip: command not found
When I tried to follow the installation instructions for pip, I ran into a permissions issue, which makes sense I guess, since I don't have access to the Mac's 2.7.10 Python install, nor do I really want it at this stage. So I'd like to switch Terminal to take the new Python 3.5.2 as my default one, in which case I guess I won't have pip permissions issues...)
Thanks in advance for any help folks!
The safest solution is to create a virtual environment running python 3 and use it as development environment. Check the following links:
https://docs.python.org/3/library/venv.html
http://docs.python-guide.org/en/latest/dev/virtualenvs/

Can't open a Python interpreter in Spyder 2.2.5 (Ubuntu 14.04)

A few weeks ago, I opened Spyder and found that I could not interact with the default Python interpreter console, which appeared greyed-out (screenshot below).
Consequently, I am not able to run Python scripts from within the IDE. I have tried rebooting my machine, opening new interpreters and IPython interpreters, but the result is always the same. I recently tried running spyder --reset in the terminal to erase my settings, but it did not help.
The only recent changes to my Python installation I made were in a virtualenv where I installed Python3, but I am not convinced this is related.
I had the same issue, also after updating packages. I solved the problem by updating Spyder:
sudo pip install --upgrade spyder

Heroku not picking up updated django-bouncer package

I had an old version of django-bouncer that required hashcompat, which is now deprecated. Since I was getting errors telling me this, I did pip uninstall django-bouncer, then installed the version upgraded for Django 1.6 (it uses hashlib instead of hashcompat) using pip install https://github.com/shelfworthy/django-bouncer/archive/master.tar.gz (I also re-added it to my requirements.txt file).
Locally, this is working fine. However, when I push to Heroku, I'm still getting the error "No module named hashcompat."
I tried doing a git push heroku master --force, but that didn't resolve the problem. Then I reset the app by doing heroku repo:reset -a <myappname>, followed by did a new git push heroku master. Unfortunately, I'm still getting the error on my Heroku app.
How can I make Heroku get the upgrade of django-bouncer?
What you should do is this:
Firstly, install django-bouncer's latest release locally on your laptop (you can do this by running pip install -U django-bouncer.
Next, figure out what the latest version is on your laptop, by running: pip freeze | grep django-bouncer. You should see something like: django-bouncer==x.x.x.
Lastly, edit your project's requirements.txt file and add django-bouncer=x.x.x, then push this change to Heroku. This will force Heroku to detect what specific version of django-bouncer is required, and install it for you.
Hope that helps!
It's possible you are running afoul of Heroku's package cache; it sees django-bouncer is already installed and doesn't bother to install it again. But, you can't uninstall it either.
I recall there's a bit of a hack to get around this: Heroku will wipe out its package cache if you change the version of Python you are using. So if you are using, say, 2.7.6, edit your runtime.txt to change it to python-3.4.0. If you are already using a 3.x branch, do the opposite. It's not important that your application actually works on the version you're changing it to -- deploy once, and change it back. That should wipe out your package cache entirely, at which point you'll be good to go.

importerror: No module named django

I installed python 2.6 alongside my mac's 2.5.2 version. As soon as I did, python2.6 manage.py runserver failed because it couldn't find django.core.management.
From a shell, import django returns importerror: No module named django.
Why?
Did you reinstall Django?
This happens when I install side by side versions of Python on Gentoo. Whenever I install a new version, I have to either reinstall the new ones or make a symlink to the old site-packages.
Because each installation of Python uses its own directory to store libraries. On a Mac, they are in /Library/Python/2.x/site-packages/. Presumably you originally installed Django in the 2.5 directory, but it isn't yet in the 2.6 one. You can symlink it there if you want to, or reinstall it using the new version.
Add site-packages to PYTHONPATH:
export PYTHONPATH="/home/jerome/bin/django-1.1/lib/python2.6/site-packages:$PYTHONPATH"
Worked on Ubuntu, with a python/django virtual environment using virtualenv and pip.
Source: http://benfsayer.com/importerror-no-module-named-django-core-management/
I use Bitnami's Django installer, and this happened for me when I wasn't in their custom shell, which I believe sets related python path environment variables. I ran ./use_djangostack in the root of the Bitnami package and then was successful running the server again.