using virtualenv (Part 2)
In Part 1 we saw how to use virtualenv.
Now let’s see how to colorize the bash prompt and how to activate a virtual environment easily.
Colorize the prompt
When a virtual env. is activated, the prompt changes. However, this change is not very visible because it’s not colorized. The name of the virtual env. should be printed with a different color, thus it would be visible immediately if an env. is activated or not.
Fortunately someone else also had this problem :) Here I found an excellent solution. I only changed the PS1 line the following way:
PS1="${PYTHON_VIRTUALENV}${GREEN}\u@\h ${YELLOW}\w${COLOR_NONE} ${BRANCH}${PROMPT_SYMBOL} "
This way the prompt and the cursor are in one line. I made a fork, my slightly modified version is available here.
Update (20131001): I updated the script above to support light background too. Instructions are in the header in a comment.
Usage
Save the file above as ~/.bash_prompt
and add the following line to the end of your ~/.bashrc
:
source ~/.bash_prompt
The resulting prompt is way cooler than the default bash prompt, thus you can use it even if you don’t work with virtual environments!
See the end of the post for a screenshot.
Activating a virtual environment easily
The standard way for activating a virtual env. is to source the script “activate
“:
jabba@jabba-uplink ~/python/mystuff $ ls -al total 24 drwxrwxr-x 3 jabba jabba 4096 Sep 21 16:16 . drwxrwxr-x 234 jabba jabba 12288 Sep 21 15:58 .. -rwxrw-r-- 1 jabba jabba 53 Sep 21 16:16 hello.py drwxrwxr-x 6 jabba jabba 4096 Sep 21 16:13 venv jabba@jabba-uplink ~/python/mystuff $ . venv/bin/activate [venv] jabba@jabba-uplink ~/python/mystuff $
However, using the command “workon venv
” would be much easier. I wanted to do it with an alias, but bash aliases do not accept arguments. There is a workaround: use functions instead.
Add the following lines to your ~/.bashrc
:
func_workon() { if [[ -z "$1" ]] then echo "Usage: workon <venv>" else . $1/bin/activate fi } alias workon=func_workon alias workoff='deactivate'
Now you can activate a virtual env. much easier:
jabba@jabba-uplink ~/python/mystuff $ ls -al total 24 drwxrwxr-x 3 jabba jabba 4096 Sep 21 16:16 . drwxrwxr-x 234 jabba jabba 12288 Sep 21 15:58 .. -rwxrw-r-- 1 jabba jabba 53 Sep 21 16:16 hello.py drwxrwxr-x 6 jabba jabba 4096 Sep 21 16:13 venv jabba@jabba-uplink ~/python/mystuff $ workon venv [venv] jabba@jabba-uplink ~/python/mystuff $
For deactivation use the command “deactivate
“, or my alias “workoff
“.
Next step
There is virtualenvwrapper, which is “a set of extensions to Ian Bicking’s virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.”
I haven’t used it yet.
using virtualenv (Part 1)
Here I won’t explain what virtualenv is. There are lots of tutorials on this topic (see the “virtualenv” subsection here for instance, or this SO thread).
This post is about the basic usage of virtualenv.
Step 0 (20131029)
Before doing anything, create the file ~/.pip/pip.conf
and edit it:
[install] download-cache = ~/.pip/download_cache
This way each version is downloaded only once.
Update (20140807): Maybe using a cache is not that good idea. It happened to me that a new version came out of a package that I wanted to install but because of the cache, it installed an older version! I didn’t understand why I still had an out-of-date version. So I removed the ~/.pip
folder entirely. I don’t recommend using a cache.
Installation
Install virtualenv:
sudo pip install virtualenv
Trying out
Create a directory where you want to store your project:
$ cd ~/python $ mkdir mystuff $ cd mystuff
Let’s create here a virtual environment:
$ virtualenv venv
You will get a subdirectory called “venv
“, which contains a clean Python virtual environment. Activate it with the following command:
$ . venv/bin/activate
Notice that the prompt changes: now it includes the name of the virtual environment. If you call the command “python
” or “pip
“, the binaries in the virtual environment will be called.
Running example:
jabba@jabba-uplink:~$ cd python jabba@jabba-uplink:~/python$ mkdir mystuff jabba@jabba-uplink:~/python$ cd mystuff/ jabba@jabba-uplink:~/python/mystuff$ ls -al total 16 drwxrwxr-x 2 jabba jabba 4096 Sep 21 15:58 . drwxrwxr-x 234 jabba jabba 12288 Sep 21 15:58 .. jabba@jabba-uplink:~/python/mystuff$ virtualenv venv ... jabba@jabba-uplink:~/python/mystuff$ ls -al total 20 drwxrwxr-x 3 jabba jabba 4096 Sep 21 15:58 . drwxrwxr-x 234 jabba jabba 12288 Sep 21 15:58 .. drwxrwxr-x 6 jabba jabba 4096 Sep 21 15:58 venv jabba@jabba-uplink:~/Dropbox/python/mystuff$ . venv/bin/activate (venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ which python /home/jabba/Dropbox/python/mystuff/venv/bin/python (venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ which pip /home/jabba/Dropbox/python/mystuff/venv/bin/pip
To stop using a virtual environment, use the command “deactivate
“.
Example
Say I have the package “requests” globally installed (with “sudo pip install requests
“). When I launch the python shell, this module can be imported.
However, when you activate a clean virtual environment, globally installed 3rd party packages are not available. They must be installed with the virtual environment’s “pip
” command, and they will be installed inside the “venv
” directory. This is the real advantage of virtualenv: virtual environments are completely isolated from each other and you can install different package versions, there won’t be any conflict.
Running example:
jabba@jabba-uplink:~/Dropbox/python/mystuff$ which python /usr/bin/python jabba@jabba-uplink:~/Dropbox/python/mystuff$ python Python 2.7.4 (default, Apr 19 2013, 18:28:01) [GCC 4.7.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. Persistent session history and tab completion are enabled. >>> import requests >>> jabba@jabba-uplink:~/Dropbox/python/mystuff$ . venv/bin/activate (venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ which python /home/jabba/Dropbox/python/mystuff/venv/bin/python (venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ python Python 2.7.4 (default, Apr 19 2013, 18:28:01) [GCC 4.7.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. Persistent session history and tab completion are enabled. >>> import requests Traceback (most recent call last): File "", line 1, in ImportError: No module named requests >>> (venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ which pip /home/jabba/Dropbox/python/mystuff/venv/bin/pip (venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ pip install requests Downloading/unpacking requests Downloading requests-1.2.3.tar.gz (348kB): 348kB downloaded Running setup.py egg_info for package requests Installing collected packages: requests Running setup.py install for requests Successfully installed requests Cleaning up... (venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ python Python 2.7.4 (default, Apr 19 2013, 18:28:01) [GCC 4.7.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. Persistent session history and tab completion are enabled. >>> import requests >>> (venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$
Where to put the project files
Store your project files (in this example) in the “~/python/mystuff
” directory. If the virtual environment is activated, then the modules installed in the virtual environment are available for your project:
(venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ ls -al total 24 drwxrwxr-x 3 jabba jabba 4096 Sep 21 16:16 . drwxrwxr-x 234 jabba jabba 12288 Sep 21 15:58 .. -rwxrw-r-- 1 jabba jabba 53 Sep 21 16:16 hello.py drwxrwxr-x 6 jabba jabba 4096 Sep 21 16:13 venv (venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ cat hello.py #!/usr/bin/env python import requests print "hello" (venv)jabba@jabba-uplink:~/Dropbox/python/mystuff$ ./hello.py hello
We installed “requests” in the previous step, and it’s nicely available for the project. If you uninstall requests from the virtual environment with “pip uninstall requests
” and try to run hello.py
, you will get an import error message.
In Part 2 we will see how to colorize the prompt and how to activate a virtual environment easily.
Python from command line
Problem
You want to calculate something with Python quickly, from the command line. You might even want to use Python in a bash script to produce some result.
Solution
$ python -c "print(2*3)" 6
Storing the result in a variable:
$ X=`python -c "print(2*3)"` $ echo $X 6
Thanks to Tajti A. for the tip.
Update (20170803)
You can also pass a bash variable to the embedded Python:
VAL="cat dog cat" NEW=`python3 -c "print('$VAL'.replace('dog', 'wolf'))"` echo $NEW
Output: “cat wolf cat”.
Anaconda Python distribution
See https://store.continuum.io/cshop/anaconda/.
“Completely free enterprise-ready Python distribution for large-scale data processing, predictive analytics, and scientific computing.”
It has the advantage that it installs everything in a specified directory. Say you need to use a machine where you don’t have root permission and/or this machine has an old Python version. Just install Anaconda and use the latest Python version it is shipped with.
Update (20160208)
I had Anaconda 2.3 and 2.5 came out a few days ago. How to upgrade to the new distribution?
$ conda update --all $ conda install anaconda=2.5 $ conda update --all
Python + Excel
Links
Both projects have a short video that demonstrates the basic usage.
Python vs Java in relation to logos
Emanuel Couto explains the difference between Python and Java, comparing the logos of the two languages:
“I read almost all comments and I there is one point that all of you missed! The logo is important for productivity!
The Python logo is obviously a snake. The name of the language, however, was based on Monty Python. So what this means is that you are going to have good laughs when the python bytes you. Specially when the code is too long to debug or understand.
The Java logo is a cup of coffee. What this means is that you are going to need coffee when programming in Java or you will fall asleep. In fact you are going to need loads of coffee when your code is near production stage. BTW I don’t drink coffee, so my job is twice as hard.
Jython is when you give coffee to a snake. Jython users are people that drank toomuch coffee and over time it doesn’t have any effect anymore. Since the python needs to sleep, the solution is giving coffee so that Java programmers can also have laughs at late hours of the day.
And that folks is a true story.” (source)
Python APIs
How to ignore an exception — the elegant way
This idea was presented by Raymond Hettinger at PyCon US 2013. He is talking about it at 43:30: http://www.youtube.com/watch?v=OSGv2VnC0go.
Problem
You want to ignore an exception.
Solution 1: the classical way
Say you want to delete a file but it’s not sure it exists.
try: os.unlink('somefile.txt') except OSError: pass
That is, if the exception occurs, we do nothing.
Solution 2: the elegant way
with ignored(OSError): os.unlink('somefile.txt')
Its source code in Python 2.7:
from contextlib import contextmanager @contextmanager def ignored(*exceptions): try: yield except exceptions: pass
This is part of Python 3.4, thus in Python 3.4 all you need is this line:
from contextlib import ignored
See the docs here.
Standard modules as standalone programs
The following tips are from this presentation.
Encode a file in base64:
python -m base64 some_file
Open a URL in the browser:
python -m webbrowser http://google.com
List of functions in a file:
python -m pyclbr some_file.py python -m pyclbr module_name
See the rest of the stdlib modules in /usr/lib/python/
.
check indentation
python -m tabnanny file.py
You must be logged in to post a comment.