300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > windows和linux中搭建python集成开发环境IDE——如何设置多个python环境

windows和linux中搭建python集成开发环境IDE——如何设置多个python环境

时间:2022-07-06 16:50:35

相关推荐

windows和linux中搭建python集成开发环境IDE——如何设置多个python环境

本系列分为两篇:

1、【转】windows和linux中搭建python集成开发环境IDE

2、【转】linux和windows下安装python集成开发环境及其python包

3、windows和linux中搭建python集成开发环境IDE——如何设置多个python环境

Install Python packages on Ubuntu 14.04

fromchris' sandbox

In this post I will document my setup of Python 2.7.6 in Ubuntu 14.04. Of course, the base Python is installed by default, both Python 2.7.6 and Python 3.4. Try the following in the terminal:

$ python --versionPython 2.7.6$ python2 --versionPython 2.7.6$ python3 --versionPython 3.4.0

As you can see, usingpythonpoints to Python 2.7.6 by default. However,python2andpython3can be used to access the desired version. I will focus on installing packages for Python 2.7.6 here.

Strategy

In the past I have installed Python packages:

Using the Ubuntu repository:

$ sudo apt-get install packagename

Or, from a git/svn repository:

$ sudo python setup.py install

Approach 1has many advantages for Python users that don’t need to have the latest versions of every package. In particular, all of the package dependencies including other Python packages, linear algebra libraries, etc. are also installed automatically. As a result, if you are new to Ubuntu and Python, strategy 1 is the way to go.

I will take a different tact, usingpipto install, upgrade, and remove packages. Also, I will install all Python packages as auser, that is, no use ofsudo. This makes it easy to use the same install procedure on a machine where I don’t have sudo privileges–say an Ubuntu cluster. However, I will needsudoto install non-Python libraries, Fortran compilers, etc. that the Python packages employ. On a cluster, the SysAdmin would have to to do this part for me and other users.

–start edit: , June 1st –

Recently use of pip on Ubuntu 14.04 has started to issue a warning that ends withInsecurePlatformWarning. After some searching around, I’ve found that this is related to SSL and the urllib3 in Python 2.7.6, the version on Ubuntu 14.04–see here if you want the details. As suggested in the discussion linked above, this can be fixed with the following installs (I’ll use the –user switch, as in the examples below)

$ pip install --user pyopenssl ndg-httpsclient pyasn1

With that we’re secured and the warnings go away. If you are just starting out, try installing pip, as below, and return to this install if use of pip gives you warnings.

–end edit: , June 1st –

pip

Of course, the starting point is to getpipinstalled. Official instructions are also available forinstalling pip.pipdepends on setuptools, but we can install both using theget-pip.pyscript, as described at the install link. To be concrete, I did the following:

$ cd ~/Downloads$ curl -O https://bootstrap.pypa.io/get-pip.py$ python get-pip.py --user

If you don’t havecurlinstalled, this can be remedied using:

$ sudo apt-get install curl

Because we have chosen local installation, the path~/.local/binhas to be added to our path. To do that, add the following to the end of your~/.bashrcfile:

# include .local/bin for local python scriptsexport PATH=~/.local/bin:$PATH

Then, source~/.bashrc:

$ source ~/.bashrc

Try the following to see if you get similar results and to make sure the basic setup is working:

$ which pip/home/cstrelioff/.local/bin/pip$ pip --versionpip 1.5.6 from /home/cstrelioff/.local/lib/python2.7/site-packages (python 2.7)

Of course,your usernameshould be in the path, but the output should look something like the above.

virtualenv

Another major tool for Python 2.7 project management isvirtualenv. This package allows the user to create manyvirtualPython environments, with different packages installed, and toactivateanddeactivethese environments whenever the user desires. This is extremely useful for developers who want to create a minimal environment for their application.

Thevirtualenvinstallation is simple withpip(again, I’m doing a user install with no sudo):

$ pip install --user virtualenv

To test it out, see if you get something like the following:

$ virtualenv --version1.11.6$ pip show virtualenv---Name: virtualenvVersion: 1.11.6Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires:

Now thatvirtualenvis installed, there will be two paths forward for the rest of the Python installs:

Keep installing as a user –I’ll use this approachfor the reasons discussed above.If you have admin permissions you can install all packages globally using a command like:

$ sudo pip install packagename

Create a virtual environment and install everything there to have a completely isolated Python environment – seevirtualenv and virtualenvwrapper on Ubuntu 14.04for an example of how to take this approach.

Ubuntu dependencies

A variety of Ubuntu-specific packages are needed by Python packages. These are libraries, compilers, fonts, etc. I’ll detail these here along with install commands. Depending on what you want to install you might not need all of these.

General development/build:

$ sudo apt-get install build-essential python-dev

Compilers/code integration:

$ sudo apt-get install gfortran$ sudo apt-get install swig

Numerical/algebra packages:

$ sudo apt-get install libatlas-dev$ sudo apt-get install liblapack-dev

Fonts (for matplotlib)

$ sudo apt-get install libfreetype6 libfreetype6-dev

More fonts (for matplotlib on Ubuntu Server 14.04– see comment at end of post) – added /03/06

$ sudo apt-get install libxft-dev

Graphviz for pygraphviz, networkx, etc.

$ sudo apt-get install graphviz libgraphviz-dev

IPython require pandoc for document conversions, printing, etc.

$ sudo apt-get install pandoc

Tinkerer dependencies

$ sudo apt-get install libxml2-dev libxslt-dev zlib1g-dev

That’s it, now we start installing the Python packages.

numpy

numpyis one of the fundamental numerical packages in Python. To install usingpiptype:

$ pip install --user numpy

This will result in a fair amount of compiling followed by a note that the package was successfully installed. If not, make a note of the error. Often this results from not having libraries and/or compilers installed (see above).

Information about the installation location and the version can be obtained with the following:

$ pip show numpy---Name: numpyVersion: 1.8.1Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires:

You should also be able to start python at the terminal andimportnumpywithout complaint:

Python 2.7.6 (default, Mar 22 , 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as np >>> print np.__version__ 1.8.1 >>> exit()

scipy

scipyhas many useful mathematical utilities, complementingnumpy. Installation is accomplished with:

$ pip install --user scipy

Again, expect lots of compiling! As withnumpy, try:

$ pip show scipy---Name: scipyVersion: 0.14.0Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires:

and, loading python:

Python 2.7.6 (default, Mar 22 , 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import scipy >>> print scipy.__version__ 0.14.0 >>> exit()

matplotlib

matplotlibis one of the main plotting packages for Python and many other packages use the utilities. Install with:

$ pip install --user matplotlib

matplotlib复杂一点,可能直接上面的操作会失败:需要先安装其依赖的包libpng和freetype,根据提示缺啥就补安装啥即可:

安装libpng:sudo apt-get install libpng-dev

安装freetype:

cd ~/Downloads

wget http://download./releases/freetype/freetype-2.4.10.tar.gz

tar zxvf freetype-2.4.10.tar.gz

cd freetype-2.4.10/

./congfigure

make

sudo make install

If you look carefully, the completion of the installation will say:

Successfully installed matplotlib python-dateutil tornado pyparsing nosebackports.ssl-match-hostnameCleaning up...

So,matplotlibinstalls a variety of Python-dependencies. As usual, try:

$ pip show matplotlib---Name: matplotlibVersion: 1.3.1Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires: numpy, python-dateutil, tornado, pyparsing, nose

Finally try a simple plot:

Python 2.7.6 (default, Mar 22 , 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import matplotlib.pyplot as plt >>> plt.plot([1,2,3,4]) [<matplotlib.lines.Line2D object at 0x7f13a8571890>] >>> plt.ylabel('some numbers') <matplotlib.text.Text object at 0x7f13a85c47d0> >>> plt.show() >>> exit()

A plot should open in a new window whenplot.show()is executed.

sympy

sympyis a computer algebra system for Python. Install withpipusing:

$ pip install --user sympy

Again, installation information frompipis obtained with:

$ pip show sympy---Name: sympyVersion: 0.7.5Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires:

Finally, following thesympy tutorial, start Python and try:

Python 2.7.6 (default, Mar 22 , 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from sympy import symbols >>> x, y = symbols('x y') >>> expr = x + 2*y >>> expr x + 2*y >>> expr + 1 x + 2*y + 1 >>> expr - x 2*y >>> exit()

Cool!

IPython

Next, we installIPython(including notebooks), which has become a major tool for sharing python projects in an interactive format. To install we use:

$ pip install --user ipython[notebook]

At the end, we get the message:

Successfully installed ipython jinja2 pyzmq markupsafeCleaning up...

showing that jinja2, pyzmq and markupsafe have also been installed. Get install information frompip:

$ pip show ipython---Name: ipythonVersion: 2.1.0Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires:

Now, try:

$ ipython

which launches theIPythonterminal. Notice theIPythonversion is provided and the prompt looks different from the normal>>>Python prompt (see theIPythondocumentation for more information):

Python 2.7.6 (default, Mar 22 , 22:59:56)Type "copyright", "credits" or "license" for more information.IPython 2.1.0 -- An enhanced Interactive Python.? -> Introduction and overview of IPython's features.%quickref -> Quick reference.help-> Python's own help system.object? -> Details about 'object', use 'object??' for extra details.In [1]: import numpy as npIn [2]: print np.__version__1.8.1In [3]: exit()

Finally,IPythonnotebook can be launched with the command:

$ ipython notebook

This launches a web browser and you should see theIPythonnotebook interface. You can create a new notebook and work away. To shutdown the server, back at the terminal where you launched the notebook, typecntrl-Cand thenywhen prompted:

Shutdown this notebook server (y/[n])? y-06-04 16:29:04.033 [NotebookApp] CRITICAL | Shutdown confirmed -06-04 16:29:04.033 [NotebookApp] Shutting down kernels

That’s it, you’re now anIPythonnotebook user!

pygraphviz

pygraphvizis a Python interface to thegraphvizvisualization code that can be used by itself but is also employed bynetworkxand other packages. Be sure thatgraphvizand its developer libraries are installed (see Ubuntu Dependencies above) and installpygraphvizusing:

$ pip install --user pygraphviz

Get install information frompip:

$ pip show pygraphviz---Name: pygraphvizVersion: 1.2Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires:

Also, try:

Python 2.7.6 (default, Mar 22 , 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pygraphviz >>> print pygraphviz.__version__ 1.2 >>> exit()

networkx

networkxis a Python package for building, analyzing, and visualizing graphs/networks. There are a variety of dependencies, all of which we have installed above. So, install with:

$ pip install --user networkx

Get install information frompip:

$ pip show networkx---Name: networkxVersion: 1.8.1Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires:

Try a simple example:

Python 2.7.6 (default, Mar 22 , 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import networkx as nx >>> G = nx.Graph() >>> G.add_edge(1,2) >>> G.add_edge(2,3) >>> import matplotlib.pyplot as plt >>> nx.draw(G) >>> plt.show() >>> exit()

Withmatplotlibandpygraphvizinstalled (see above), this code should create a very simple graph and show it in a new window whenplt.show()is executed.

pandas

pandasis a Python packaged focused on data – reading, writing, manipulating, etc. There are a variety ofpandas dependencies: required, recommended and optional. We’ll focus on the first two categories.

The required dependencies arenumpy(installed above),python-dateutil(installed above withmatplotlib), andpytz(we will letpipinstall withpandas). However, let’s install the recommended dependencies:

numexpr

$ pip install --user numexpr

After install we get:

$ pip show numexpr---Name: numexprVersion: 2.4Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires: numpy

bottleneck

$ pip install --user Bottleneck

After install we get:

$ pip show Bottleneck---Name: BottleneckVersion: 0.8.0Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires:

We can also import both packages in Python and print the package version to make sure that basic usage seems okay:

$ python -c "import numexpr;print numexpr.__version__"2.4$ python -c "import bottleneck;print bottleneck.__version__"0.8.0

Finally, forpandas, we install the main package:

$ pip install --user pandas

After some downloading and compiling we get (showing that both pandasandpytz were installed, as expected):

Successfully installed pandas pytzCleaning up...

Usepipto check the installation information:

$ pip show pandas---Name: pandasVersion: 0.14.0Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires: python-dateutil, pytz, numpy

Note: if you importpandas, an error aboutopenpyxl(a package for working with Excel files) will be issued:

Python 2.7.6 (default, Mar 22 , 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pandas /home/cstrelioff/.local/lib/python2.7/site-packages/pandas/io/excel.py:626: UserWarning: Installed openpyxl is not supported at this time. Use >=1.6.1 and <2.0.0. .format(openpyxl_compat.start_ver, openpyxl_compat.stop_ver)) >>> exit()

The error says thatopenpyxlneeds to be at least version 1.6.1 and less than 2.0.0.Strange, this package is listed as optional. Oh well, let’s install an appropriate version. If we just usepipto install the current version it will be too high. So, I installed as follows:

openpyxl 1.8.6

$ pip install --user openpyxl==1.8.6

This install forces the use an appropriate version. Now, try importingpandasand we get:

Python 2.7.6 (default, Mar 22 , 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pandas >>> print pandas.__version__ 0.14.0 >>> import openpyxl >>> print openpyxl.__version__ 1.8.6 >>> exit()

Yay(!) we can importpandas(and openpyxl) without complaints.

Finally, before leavingpandas, I will mention that there are a variety ofoptional pandas dependenciesthat you might want to consider as well. I won’t consider them in this post.

pymc

pymcis a really nice MCMC package for Python. I have used it on several projects with great success. Installation withpipfollows the usual format:

$ pip install --user pymc

Get install information:

$ pip show pymc---Name: pymcVersion: 2.3.2Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires:

Starting Python you should also be able to get:

Python 2.7.6 (default, Mar 22 , 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pymc >>> print pymc.__version__ 2.3.2 >>> exit()

statsmodels

statsmodelsprovides some nice statistics methods. Before installingstatsmodelsitself, we must install dependencies, which will likely be usesul in any case:patsyandcython.

patsy: is a package for describing statistical models in R-like format. Install with:

$ pip install --user patsy

We can see wherepipinstalledpatsy:

$ pip show patsy---Name: patsyVersion: 0.2.1Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires: numpy

and try importingpatsyin a Python session:

Python 2.7.6 (default, Mar 22 , 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import patsy >>> print patsy.__version__ 0.2.1 >>> exit()

cython: allows for wrapping of c++ code. Install with:

$ pip install --user Cython

Check withpip:

$ pip show Cython---Name: CythonVersion: 0.20.1Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires:

and importing in a Python session:

Python 2.7.6 (default, Mar 22 , 22:59:56) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import cython >>> print cython.__version__ 0.20.1 >>> exit()

Finally, installstatsmodelswithpip:

$ pip install --user statsmodels

Show install info withpip:

$ pip show statsmodels---Name: statsmodelsVersion: 0.5.0Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires:

and try an import:

Python 2.7.6 (default, Mar 22 , 22:59:56)[GCC 4.8.2] on linux2Type "help", "copyright", "credits" or "license" for more information. >>> import statsmodels >>> print statsmodels.__version__ 0.5.0 >>> exit()

Okay, that’spatsy,cythonandstatsmodels.

CMPy

CMPyis a package for Computational Mechanics in Python developed in the Crutchfield Lab at UC Davis. Currently the package is developed, using git for version control, but is not publicly available. However, I will document the install here because:

It’s useful for people at UCD (or collaborating with people at UCD)This is an example of installation of a Python package in a folder on the local machine

I start by showing that I have cloned theCMPypackage to the~/gitlocal/cmpy/directory. You can see thesetup.pyfile when I show the directory contents:

$ ls ~/gitlocal/cmpy/apps build CHANGES.txt cmpy data docs gallery LICENSE.txt MANIFEST.in old_doc pylintrc README.txt scripts setup.py src

We do the install withpip, using the-eswitch to show the location of the package code:

$ pip install --user -e ~/gitlocal/cmpy/Obtaining file:///home/cstrelioff/gitlocal/cmpyRunning setup.py (path:/home/cstrelioff/gitlocal/cmpy/setup.py) egg_info for package from file:///home/cstrelioff/gitlocal/cmpyInstalling collected packages: CMPyRunning setup.py develop for CMPyCreating /home/cstrelioff/.local/lib/python2.7/site-packages/CMPy.egg-link (link to .) Adding CMPy 1.0dev to easy-install.pth file Installed /home/cstrelioff/gitlocal/cmpy Successfully installed CMPy Cleaning up...

Note that the path to theCMPydirectory is added toeasy-install.pth, a file that Python consults to findCMPy. Finally, we show thepipinformation:

$ pip show cmpy---Name: CMPyVersion: 1.0devLocation: /home/cstrelioff/gitlocal/cmpyRequires:

Again, note that the location is~/gitlocal/cmpy/, instead of~/.local/lib/python2.7/site-packages/, due to the-etag. This is why the addition to theeasy_install.pthfile (above) was needed.

Edit:Aug 21st,

A note on updating this local installation is in order. Recently a change in code was made that affected underlyingc codethat is incorporated using cython. I pulled the repository changes using:

$ cd ~/gitlocal/cmpy/$ git pull

To try and update the install I did:

$ pip install --user -e ~/gitlocal/cmpy/

This ran thesetup.pyfile but didnotrecompile the modified c code. To get this to work I had to remove thebuilddirectory, build in place and install again:

$ cd ~/gitlocal/cmpy/$ rm -r build/$ python setup.py build_ext -i --cython$ pip install --user -e ~/gitlocal/cmpy/

Is there a better way to do this? Let me know in the comments below.

restview

restviewis a Python package that processesreStructuredTextand launches a web browser for viewing. Each time the browser is refreshed, the underlyingrstdocument will be re-processed and displayed– very nice for working on Python docmentation or anyrstdocument. Installation goes as usual:

$ pip install --user restview

We can see what was installed:

$ pip show restview---Name: restviewVersion: 2.0.5Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires: docutils, pygments

As you can see from above,docutilsandpygmentswill be installed if they are not already installed.

To process anrstdocument namedtest.rsttype:

$ restview test.rst

Checkrestviewfor more examples.

tinkerer

tinkereris a blogging environment for Pythonistas that is built onSphinx, a Python documentation tool. Blog entries are written inreStructuredTextand rendered as static html. Of course, this is also the tool I use for this blog. Before moving to our usualpipinstall, we have to take care of someUbuntu 14.04 Python dependencies. Assuming these requirements are available,tinkereris installed with the usual:

$ pip install --user tinkerer

We can check the install information with:

$ pip show tinkerer---Name: TinkererVersion: 1.4.2Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires: Jinja2, Sphinx, Babel, pyquery

Note that requirementsJinja2,Sphinx,Babelandpyqueryare also installed automatically. A quick start to getting a blog up and running (at least the generation of posts, pages and generating the html output) is availablehere.

Pweave

Pweaveis a tool for literate programming with Python. This tool allows me to write blog posts about Python using a.Pnwfile that containsreStructuredText, along with specialPweavecommands, and have the Python code evaluated and output included in the.rstoutput file–see the example here. This is a really nice tool to avoid typos in code and to make sure that what you’re talking about actually works! I should note thatIPythonnotebooks can also do this by exporting toreStructuredText. In any case, I will trying out both of these tools for future posts.

The install ofPweavegoes as usual:

$ pip install --user Pweave

Check the install with:

$ pip show Pweave---Name: PweaveVersion: 0.21.2Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires:

scikit-learn

scikit-learnis the probably the most well-known and feature-complete package for machine learning tasks in Python. There are a number of dependencies that need to be installed (numpy,scipy, python-dev, etc seescikit-learn installationfor more information) that have already been installed above. So, we install usingpip, as usual:

$ pip install --user scikit-learn

Then we can check the installed version and location using:

$ pip show scikit-learn---Name: scikit-learnVersion: 0.15.1Location: /home/cstrelioff/.local/lib/python2.7/site-packagesRequires:

That’s it, machine-learn away!

Posted by Chris Strelioff Tags:python 2.7,ubuntu 14.04,python,my ubuntu setup,pip,virtualenv,numpy,scipy,matplotlib,sympy,ipython,pygraphviz,networkx,pandas,numexpr,bottleneck,openpyxl,pymc,statsmodels,patsy,cython,cmpy,restview,tinkerer,pweave,scikit-learn «Installing Octave on Ubuntu 14.04

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。