I installed Manjaro Linux on one of my laptops, just to try something new. I’ve been using it for a week and I like it so far :) On my older laptop it runs smoother than Ubuntu.
Anyway, Manjaro switched to Python 3.x, that’s the default, thus “python” points to Python 3. I use Ubuntu on my other machines where Python 2 is the default. I would like to modify my scripts (at least some of them) to run on both systems.
For instance, in Python 2.x you call “raw_input”, while this function was renamed to “input” in Python 3.x.
Well, since January 2014 I start all my new scripts with this line:
from __future__ import (absolute_import, division, print_function, unicode_literals)
It ensures a nice transition from Python 2 to Python 3.
To solve the “raw_input” problem, you can add these lines:
import sys if sys.version_info >= (3, 0): raw_input = input
You can continue using “raw_input”, but if it’s executed with Python 3.x, “raw_input” will point to the “input” function.
Of course, the ideal solution would be to switch to Python 3, but I’m not ready for that yet :)
Sometimes you need to install a software with “
python setup.py install“. However, if you want to get rid of it, there is no “python setup.py uninstall” or “python setup.py remove”. Stupid, but true.
I found the solution here. In short:
# install: $ (sudo) python setup.py install # uninstall: $ pip freeze | grep PATTERN_BASED_ON_PACKAGE_NAME # then remove with pip: $ (sudo) pip uninstall PACKAGE_NAME
On my Linux box the default Python is version 2.7. I installed
bpython system-wide, thus when I launch
bpython, it uses Python 2.7.
How could I have another
bpython that uses Python 3? I would like to have both though. Sometimes I need Python 2, sometimes Python 3, and I would like to use the
bpython shell with them.
bpython in a Python 3 virtual environment. That is, create a Python 3 environment, and install
bpython in it with
bpython installation will use Python 3. Then use the absolute path of this
bpython executable. And no, you don’t have to activate the virt. env. each time you want to use this
In a previous post I showed how to patch
virtualenvwrapper to make it suitable for creating not only Python 2 but Python 3 environments too. Using this patched
virtualenvwrapper, the process looked like this on my machine:
$ cd $ mkdir bpython_python3 $ cd bpython_python3 $ mkvirtualenv bpython_python3 # creates a Python 3 virt. env. (bpython_python3)$ pip install bpython (bpython_python3)$ which bpython /home/jabba/.virtualenvs/bpython_python3/bin/bpython # sample; will be different in your case (bpython_python3)$ deactivate $
I suggest adding the following aliases to your
alias bpy='bpython' alias bpy3='/home/jabba/.virtualenvs/bpython_python3/bin/bpython'
Of course, you need to customize the path for
bpython with Python 2, while “
bpython with Python 3.
Django 1.7 came out ten days ago. If you look at the updated poll tutorial, you will notice that it’s written for Python 3! So far I’ve used Python 2 only, so I thought I would redo this tutorial for two reasons: (1) learn about migrations, and (2) start using Python 3.
For my Django projects I use virtualenvwrapper to manage my virtual environments, so the first question was: how to create a Python 3 virt. env. with virtualenvwrapper?
Well, maybe my installation got corrupted somehow, but I ran into this problem:
$ mkvirtualenv -p `which python3` tmpenv Running virtualenv with interpreter /usr/bin/python3 Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 8, in <module> import base64 File "/usr/lib/python3.4/base64.py", line 9, in <module> import re File "/usr/lib/python3.4/re.py", line 324, in <module> import copyreg File "/usr/local/lib/python2.7/dist-packages/copyreg.py", line 3, in <module> from copy_reg import * ImportError: No module named 'copy_reg' Error in sys.excepthook: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 53, in apport_excepthook if not enabled(): File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 24, in enabled import re File "/usr/lib/python3.4/re.py", line 324, in <module> import copyreg File "/usr/local/lib/python2.7/dist-packages/copyreg.py", line 3, in <module> from copy_reg import * ImportError: No module named 'copy_reg' Original exception was: Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/virtualenv.py", line 8, in <module> import base64 File "/usr/lib/python3.4/base64.py", line 9, in <module> import re File "/usr/lib/python3.4/re.py", line 324, in <module> import copyreg File "/usr/local/lib/python2.7/dist-packages/copyreg.py", line 3, in <module> from copy_reg import * ImportError: No module named 'copy_reg'
I posted this question to reddit (see here) and I found the following solution:
First, install pip3:
sudo apt-get install python3-pip sudo pip3 install virtualenvwrapper
Then, add the following lines to your .bashrc:
export WORKON_HOME=$HOME/.virtualenvs export PROJECT_HOME=$HOME/workspace # customize if needed export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv source /usr/local/bin/virtualenvwrapper.sh
Try if it works for you. In my case, I had to edit the file “
/usr/local/bin/virtualenv“. I changed its first line to
The consequence of changing the first line was that creating Python 3 environments became the default, but at least it works now.
Create a Python 3 virt. env.:
Create a Python 2 virt. env.:
mktmpenv -p `which python2`
If you use the command “
virtualenv“, it will also create Python 3 environments by default. Thus, “
virtualenv myproject3” will be a Python 3 env., while “
virtualenv -p python2 myproject2” will be a Python 2 env.
rotated = zip(*original[::-1])
rotated_ccw = zip(*original)[::-1]
I found this trick in this thread.
A curated list of awesome Django apps, projects and resources: https://github.com/rosarior/awesome-django.