About
RSS

Bit Focus


Troubleshooting : Upgrade pip3 in Ubuntu 16.04

Generally this post is about to setup a Python3 environment on Ubuntu 16.04. However since there seems a problem in pip so let me name it in the title directly.

I started with a fresh new Ubuntu 16.04 droplet at DigitalOcean and have run these commands.

(As non-root with sudo)

Code Snippet 0-0

$ sudo apt-get update
$ sudo apt-get install python3-pip -y
$ sudo pip3 install -U pip

So pip 10 is installed and things seem fine until here. However when I attempt to run pip3, it crashes with an unclear Python stacktrace.

Code Snippet 0-1

$ pip3 --version

Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'

It seems that when apt-installed pip upgrades a pypi pip, the pypi pip will be broken and won't work. And the idea is not to use the system-wide pip but to use a venv pip instead.

So the solution is like this.

Code Snippet 0-2

$ sudo apt-get install python3-venv
$ python3 -m venv $HOME/.venv # or any directory you like
$ export PATH=$HOME/.venv/bin:$PATH

Then the pip in the virtualenv will work.

Code Snippet 0-3

$ which python
/home_directory/.venv/bin/python
$ python --version
python 3.5.2
$ which pip
/home_directory/.venv/bin/pip

Now upgrade pip with itself.

Code Snippet 0-4

$ pip install -U pip
...
$ pip --version
pip 10.0.1 from /home_direcotry/.venv/lib/python3.5/site-packages/pip (python 3.5)

Also, you can add export PATH=$HOME/.venv:$PATH to your .bashrc or .zshrc or anything like that.

Permanent Link: /p/6 Load full text

Post tags:

 Python
 Ubuntu
 Installation

Unicode troubleshooting : strftime in Jinja2

Say, in Python2, if we need to format a datetime object with some unicode in the format, what shall we do?

The following code looks perfect

Code Snippet 0-0

# encoding=utf-8

import jinja2
import datetime

now = datetime.datetime.now()

print(jinja2.Template(u'''{{ date.strftime('%Y 年 %m 月') }}''').render(date=now))

Except that it raises a UnicodeEncodeError.

Code Snippet 0-1

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    print(jinja2.Template(u'''{{ date.strftime('%Y 年 %m 月') }}''').render(date=now))
  File "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python2.7/dist-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "<template>", line 1, in top-level template code
UnicodeEncodeError: 'ascii' codec can't encode character u'\u5e74' in position 3: ordinal not in range(128)

So what's wrong with that? The reason is that many standard libraries in Python2 don't have good support to unicode. What a cruel fact.

Well, since we have declared that the file is encoded with UTF-8, how about directly using a str instead of a unicode?

If we use only the standard library it actually works.

Code Snippet 0-2

# encoding=utf-8

import datetime

now = datetime.datetime.now()

print(now.strftime('%Y 年 %m 月'))

This would produce a desired output. So you may think let's remove the prefix u and the template rendering becomes fine, right?

Unfortunately, to do so will get you a UnicodeDecodeError like this

Permanent Link: /p/5 Load full text

Post tags:

 Python


. Back to Bit Focus
NijiPress - Copyright (C) Neuron Teckid @ Bit Focus
About this site