pages tagged Python http://meng6net.localhost/tag/Python/ <p><small>Copyright © 2005-2020 by <code>Meng Lu &lt;lumeng3@gmail.com&gt;</code></small></p> Meng Lu's home page ikiwiki Tue, 16 May 2017 23:59:39 +0000 Python division http://meng6net.localhost/journal/Python_division/ http://meng6net.localhost/journal/Python_division/ Python note programming tip Tue, 16 May 2017 23:59:39 +0000 2017-05-16T23:59:39Z <p>In Python 2, dividing two integers will yield a rounded integer result:</p> <pre><code>$ ipython2 Python 2.7.11 (default, Jan 22 2016, 08:29:18) Type "copyright", "credits" or "license" for more information. IPython 4.1.2 -- An enhanced Interactive Python. ? -&gt; Introduction and overview of IPython's features. %quickref -&gt; Quick reference. help -&gt; Python's own help system. object? -&gt; Details about 'object', use 'object??' for extra details. In [1]: 1/2 Out[1]: 0 </code></pre> <p>To get a real-number result:</p> <pre><code>In [2]: 1./2 Out[2]: 0.5 In [3]: float(1)/2 Out[3]: 0.5 </code></pre> <p>In Python 3, the default is the real-number result:</p> <pre><code>$ ipython Python 3.5.1 (default, Dec 7 2015, 21:59:10) Type "copyright", "credits" or "license" for more information. IPython 4.1.2 -- An enhanced Interactive Python. ? -&gt; Introduction and overview of IPython's features. %quickref -&gt; Quick reference. help -&gt; Python's own help system. object? -&gt; Details about 'object', use 'object??' for extra details. In [2]: 1/2 Out[2]: 0.5 In [3]: exit &gt;&gt;&gt; elapsed time 15s </code></pre> <p>In a Python script, to use Python 3 division behavior for Python 2:</p> <pre><code>import from __future__ division; print 1/2 </code></pre> How to use matplotlib in Python virtual envrionments http://meng6net.localhost/journal/how_to_use_matplotlib_in_Python_virtual_envrionments/ http://meng6net.localhost/journal/how_to_use_matplotlib_in_Python_virtual_envrionments/ Python computing matplotlib tip workaround Tue, 16 May 2017 23:59:39 +0000 2017-05-16T23:59:39Z <p>backend: TkAgg</p> <h2>References</h2> <ul> <li><a href= "http://matplotlib.org/faq/virtualenv_faq.html">http://matplotlib.org/faq/virtualenv_faq.html</a></li> </ul> Installing and configuring Python on Ubuntu Linux http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_Python_on_Ubuntu_Linux/ http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_Python_on_Ubuntu_Linux/ Linux Python computing configuration documentation installation note software Tue, 16 May 2017 23:59:39 +0000 2017-05-16T23:59:39Z <h2>Install system packages</h2> <pre> <code>sudo apt-get install gfortran libopenblas-base libopenblas-dev libzmq3-dev sudo apt-get install python-qt4 python-zmq </code></pre> <p>The following shew up as dependencies when I tried to install <code>matplotlib</code> in a virtual environment using <code>pip install matplotlib</code>:</p> <pre><code>sudo apt-get install libfreetype6-dev libpng12-dev </code></pre> <h2>Install Python</h2> <pre> <code>sudo apt-get install python python-pip python-dev build-essentials sudo apt-get install python3 python3-pip </code></pre> <h2>Install IPython</h2> <pre><code>sudo apt-get install ipython python-notebook </code></pre> <p>To install IPython with Python 3 [<sup id="fnref:1"><a href= "http://meng6net.localhost/tag/Python/#fn:1" rel="footnote">1</a></sup>]:</p> <pre><code>sudo apt-get install ipython3 python3-notebook </code></pre> <h2>Install Python packages</h2> <h3>Use Ubuntu's package management system <code>apt-get</code>:</h3> <pre><code>sudo apt-get install python-matplotlib sudo apt-get install virtualenv vurtualenvwrapper </code></pre> <h3>Use Python package management system <code>pip</code>:</h3> <pre><code>pip install "ipython[all]" pip install virtualenv pip install virtualenvwrapper pip install numpy pip install scipy pip install matplotlib pip install pyzmq pip install pygments </code></pre> <h2>Use Anaconda Python distribution and its package management and environment management</h2> <p>This part is same on macOS or Ubuntu Linux. See <a href= "http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_Python_on_macOS/"> installing and configuring Python on macOS</a>.</p> <h2>Other packages</h2> <h3>GraphLab, a machine-learning package</h3> <p>This part is same on macOS or Ubuntu Linux. See <a href= "http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_Python_on_macOS/"> installing and configuring Python on macOS</a>.</p> <h2>References</h2> <div class="footnotes"> <hr /> <ol> <li id="fn:1"> <p>It used to be that <code>ipython</code> has to be installed via <code>pip3</code>, but I think this has been superseded by the above as of Ubuntu 15.10 and Mar. 2016 at least:</p> <p>pip3 install ipython</p> <a href="http://meng6net.localhost/tag/Python/#fnref:1" rev="footnote">↩</a></li> </ol> </div> Installing and configuring Python on macOS http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_Python_on_macOS/ http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_Python_on_macOS/ Python computing configuration documentation installation note software Tue, 16 May 2017 23:59:39 +0000 2017-05-16T23:59:39Z <h2>History</h2> <ul> <li>2016-3: Tested on macOS 10.11.3</li> </ul> <h2>Install other softwares</h2> <ul> <li> <p>Install <a href="http://www.xquartz.org/">XQuartz</a>.</p> </li> <li> <p>Install <a href="https://tug.org/mactex/">MacTeX</a>.</p> </li> <li> <p>Install <a href="http://brew.sh/">Home brew</a>.</p> </li> <li> <p>Install <a href= "http://osxdaily.com/2014/02/12/install-command-line-tools-mac-os-x/"> Xcode and command-line tools</a>.</p> <pre><code> xcode-select --install </code></pre></li> <li> <p>Include only the necessary paths in <code>$PATH</code>:</p> <pre><code> bash$ echo $PATH /usr/local/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/Library/TeX/texbin:/usr/X11/bin:/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin </code></pre></li> </ul> <h2>Install packages</h2> <pre><code>brew install cmake </code></pre> <h2>Install Python</h2> <ul> <li> <p>Make sure you start from a clean and error-free state with Homebrew:</p> <pre><code> brew doctor </code></pre></li> </ul> <p>Check outdated packages:</p> <pre><code> brew outdated </code></pre> <p>Update:</p> <pre><code> brew update brew upgrade </code></pre> <p>Add additional repositories and re-check:</p> <pre><code> brew tap homebrew/science brew tap homebrew/python brew doctor brew update brew upgrade brew doctor </code></pre> <ul> <li> <p>Install Python:</p> <pre><code> $ brew install python python3 $ which python /usr/local/bin/python $ python --version Python 2.7.11 $ which python3 /usr/local/bin/python $ python3 --version Python 3.5.1 </code></pre></li> <li> <p>This actually also installs the package management system <code>pip</code>:</p> <pre><code> $ which pip /usr/local/bin/pip $ which pip3 /usr/local/bin/pip3 </code></pre></li> </ul> <h2>Install other system packages</h2> <pre><code> brew install libtiff brew install libjpeg brew install webp brew install py2cairo brew install cairo brew install little-cms2 brew install imagemagick --with-fftw --with-librsvg --with-x11 brew install graphviz --with-librsvg --with-x11 brew install qt pyqt brew install zmq brew install zeromq brew install gcc brew install freetype </code></pre> <h2>Install Python packages</h2> <p>Warnings: * For the same software or library, install once using <code>brew install &lt;package&gt;</code> or <code>pip install &lt;package&gt;</code> but not both. The latter is usually more up-to-date. * Do not use <code>sudo pip</code>.</p> <h3>Install with <code>brew</code></h3> <pre> <code> brew install homebrew/python/matplotlib --with-cairo --with-tex </code></pre> <h3>Alternatively, install with <code>pip</code></h3> <ul> <li> <p>Update:</p> <pre><code> pip install --upgrade pip pip install pip-review pip3 install --upgrade pip pip3 install pip-review </code></pre></li> <li> <p>Virtual environment:</p> <pre><code> pip install virtualenv pip install virtualenvwrapper </code></pre></li> <li> <p>IPython</p> <pre><code> pip install "ipython[all]" pip3 install "ipython[all]" </code></pre></li> </ul> <p>Start IPython notebook server by:</p> <pre><code> ipython notebook ipython3 notebook </code></pre> <p>and open <a href= "http://localhost:8889/tree">http://localhost:8889/tree</a> in Web browser.</p> <ul> <li> <p>Unit testing:</p> <pre><code> pip install nose </code></pre></li> <li> <p>Scientific computing:</p> <pre><code> pip install numpy pip install scipy pip install sympy python -c 'import numpy ; numpy.test();' python -c 'import scipy ; scipy.test();' </code></pre></li> <li> <p>Data management:</p> <pre><code> pip install pandas </code></pre></li> <li> <p><a href="http://www.nltk.org/">Natural Language Toolkit</a></p> <pre><code> pip install nltk </code></pre></li> <li> <p>Graphics related:</p> <pre><code> pip install snakeviz pip install pyzmq pip install pygments </code></pre></li> </ul> <p>For <code>matplotlib</code> choose only one of the following:</p> <pre> <code> brew install homebrew/python/matplotlib --with-cairo --with-tex pip install matplotlib </code></pre> <ul> <li> <p>Others:</p> <pre><code> pip install q </code></pre></li> </ul> <h2>Use Anaconda Python distribution and its package management and environment management</h2> <ul> <li>Should I use Anaconda?</li> </ul> <p>Anaconda is itself a Python distribution including Python and lots of Python packages for scientific and technical computing. It also includes a package management system as well as a environment management system. It's fairly popular and convenient to install and use for beginners. But by using <code>pip</code> as the package management system the <code>virtualenv</code> and <code>virtualenvwrapper</code> packages as the environment management system, one can live without Anaconda. Roughly speaking, Anaconda is to Python what <code>stack</code> is to Haskell.</p> <ul> <li> <p>Install Anaconda (see <a href= "http://docs.continuum.io/anaconda/install">instruction</a>). Put Anaconda <code>bin</code> and <code>man</code> paths in <code>$PATH</code> in <code>~/.bashrc</code>:</p> <pre><code> export PATH=$HOME/anaconda/bin:$PATH export MANPATH=$HOME/anaconda/share/man:$MANPATH </code></pre></li> </ul> <p>Update</p> <pre><code> conda update conda conda update anaconda </code></pre> <ul> <li> <p>Create a new environment <code>my-env</code>:</p> <pre><code> conda create -n my-env python=2.7 anaconda </code></pre></li> <li> <p>Activate the environment:</p> <pre><code> source activate my-env </code></pre></li> <li> <p>Install and/or update packages:</p> <pre><code> conda update conda conda update anaconda conda update pip conda update ipython ipython-notebook </code></pre></li> </ul> <h2>Alternative to Anaconda, set up project-specific Python environment using <code>virtualenv</code> and system Python</h2> <p>Let's install specific packages and/or specific versions of some packages in isolated environment named <code>my-env</code> for a specific project.</p> <ul> <li> <p>Create a virtual environment named <code>my-env</code>:</p> <pre><code> cd path/to/project virtualenv my-env git ignore my-env </code></pre></li> <li> <p>Activate the virtual environment:</p> <pre><code> $ source my-env/bin/activate $ echo $VIRTUAL_ENV /Users/meng/project/my-env (my-env) </code></pre></li> </ul> <h2>Other packages</h2> <h3>GraphLab, a machine-learning package</h3> <ul> <li><a href= "https://dato.com/download/install-graphlab-create-command-line.html"> https://dato.com/download/install-graphlab-create-command-line.html</a></li> </ul> <p>You can obtain a free 1-year license for educational purposes such as taking the University of Washington Machine Learning course series on coursera.org.</p> <h2>References</h2> <ul> <li><a href= "https://virtualenvwrapper.readthedocs.org/en/latest/index.html"><code> virtualenvwrapper</code></a> <ul> <li>command reference: <a href= "https://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html"> https://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html</a></li> </ul> </li> <li><a href= "https://stackoverflow.com/questions/17759740/python-under-mac-os-x-framework"> https://stackoverflow.com/questions/17759740/python-under-mac-os-x-framework</a></li> </ul>