• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python/C++inVisualStudio:AnAlternativetoMatlab/MEX

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

来自Andrew Delong的博客

http://andrewdelong.wordpress.com/2012/11/03/pythonc-in-visual-studio-an-alternative-to-matlabmex/

I spent much of my PhD working in Matlab with C++ MEX extensions. Debugging MEX extensions is frustrating: either you resort to print statements, or you wait for the IDE to launch a new Matlab every time you make a change.

With Microsoft’s release of Python Tools for Visual Studio, I decided to switch to Python with C++ extensions.


The integration is exactly what I was looking for: full debugging, auto-complete, real-time syntax checking, python console (like Matlab), and profiling (needs Ultimate Edition of Visual Studio). Python can load C++ extensions built with Visual Studio 2010/2012 just fine, even though Win64 Python is itself compiled with Visual Studio 2008; the new compilers are amust for using the wonderful features of C++11 .

Getting a Matlab-like setup for Win64 takes a few steps. For Linux, one has the option of simply installing Free 64-bit EPD Python,  a Python distribution that bundles several packages for scientific computing (plotting, matrices, Intel MKL). 64-bit EPD does not seem to be free for Windows users. So, the rest of this post is a guide to set things up from scratch.

1. Install Python

  • Download Python 2.7 X86-64 and install it to the default location.
  • Make sure C:\Python27 is in your system path: open a command-prompt and run “python”; use “quit()” to exit the interpreter.
  • Add PYTHON_PATH=C:\Python27 to your system environment variables. (This will be convenient when setting up Visual Studio projects that link with Python, e.g. a C/C++ extension module).

2. Install Numpy-MKL and SciPy

3. Install Matplotlib

  • Download the latest Matplotlib-win-amd64-python2.7 and install it.
  • Test it by starting a python interpreter and running the commands
          import matplotlob
          import matplotlib.pyplot as plt
          plt.plot([1,2,2,3])
          plt.show()

    You should see a Figure window pop up.

  • [Optional] If you are annoyed by the 4-pixel grey margin around all figure windows, openC:\Python27\Lib\site-packages\matplotlib\backends\backend_tkagg.pyand find the place where it says “borderwidth=4“, then replace this
    with “borderwidth=0, highlightthickness=0“.

4. Install Visual Studio

If you are a student or staff at a university, you can get a free license for Visual Studio Ultimate Edition (2010 or 2012) by getting a Microsoft DreamSpark account through your department. However, these steps should work just fine with the free Express Edition of Visual Studio as well. (Note that Python/C++ speed profiling is only available in Ultimate Edition).

5. Install Python Tools for Visual Studio

Download Python Tools for Visual Studio and install. Be sure to get the version for the IDE you want to use (i.e. 2010 or 2012).

To see the new Python console, go to View->Other Windows and select it. You can dock the new tool window with the rest.

Also enable the Python Debug Interactive window in Debug->Windows.

Notes

As of PTVS 2.0 Beta, mixed C++/Python debugging is available in VS 2012 or later. By default, breakpoints will only work in either Python or C++ during any one debugging session — if you debug using a Python “startup project”, breakpoints in your C++ code will be ignored for that debug session; if you debug a C++ “startup project”, your Python breakpoints will be ignored. If you want mixed debugging, you must explicitly enable it in your Python project’s Debug settings (VS2012 only).

Tip #1: Python code will run slower when debugging; I find Ctrl+F5 (run without debugging) immensely useful when I’m not planning to hit any breakpoints.

Tip #2: By default Visual Studio will break when Python exceptions are thrown. This is a problem because many Python modules use exceptions as a means of ‘normal’ control flow (bad!), so you’ll want to tell the debugger to let most exceptions slide. Go to Tools->Options->Debugging and select “Enable Just My Code”.

Then go to Debug->Exceptions and uncheck the “Thrown” column for Python

If a package imports “without debugging” but breaks when you run it “with debugging” then you may even have to disable breaking on a User-unhandled exception.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Ubuntu16.04+Cuda8.0+Cudnn+Matlab2014a+OpenCV+Caffe 吐血安装教程发布时间:2022-07-18
下一篇:
win7系统64位安装MATLAB(R2016)发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap