📄 122.html
字号:
</dl>
<p>This process creates a <tt class="monofont">helloworldmodule.so</tt> file.</p>
<p>You could also try</p>
<Pre>
gcc -c -I/usr/local/include/python1.5 helloworldmodule.c
gcc -shared helloworldmodule.o -o helloworldmodule.so
</Pre>
<h5>Dynamic Extension on Windows</H5>
<p>Next, how you can build a Dynamic Extension on Windows is illustrated.</p>
<p>
<p>
<P>
<p>
<p>
<dL>
<DD>
<B>Step 1. </b>
Create a directory in the Python top-level directory. Give it the name of your module.
<p>For example, <tt CLASs="monofont">c:\python\Python-1.5.2\pimodule</tt></p>
</DD>
<DD>
<b>Step 2. </b>
<p>Copy your <i><TT CLass="monofont">modulenamemodule.</tt></i>
<tt class="monofont">c</tt> file to this directory.</p>
</dd>
<dd>
<B>Step 3. </b>
<p>Copy the files <Tt clAss="monofont">example.def, example.dsp, example.dsw,</tt> and <Tt clASS="monofont">example.mak,</Tt> which are located at the <tt cLASS="monofont">/PC/example_nt</tt> directory of the standard distribution to your new directory. Don't forget to rename the prefix of these files in order to match the name of your module.</p>
</dD>
<DD>
<B>Step 4. </b>
<p>On each file, replace the occurrences of <tt CLASs="monofont">example</tt> with your module name.</p>
</dd>
<dd>
<b>Step 5. </b>
<p>Choose the <tt class="monofont">Build</tt> Menu option in order to generate your <I><tt ClasS="monofont">modulename.</tt></i><tT claSS="monofont">dll.</TT></p>
</dd>
</dL>
<P>A subdirectory was created underneath your working directory. This subdirectory, called <TT clasS="monofont">Release,</TT> contains your <Tt claSS="monofont">modulename.dll.</TT></p>
<p>A tool created by <a name="idx1073743970"></a>David Ascher is very useful to create Python extension modules. It uses a UNIX <tt class="monofont">Setup.in</tt> file to generate and build a Microsoft Visual C++ project. This tool is called <a naMe="idx1073743971"></a><A namE="idx1073743972"></a><a naMe="idx1073743973"></a><tT CLAss="monofont">compile.py.</tt></P>
<P>To use it, you just need to put your C module and the <TT clasS="monofont">compile.py</TT> file in the same directory, and execute the tool. When fired, the program creates a MS Visual C++ project (<Tt claSS="monofont">.dsp</TT> extension) and the workspace (<tt class="monofont">.dsw</tt> extension).<a name="idx1073743974"></a><a name="idx1073743975"></A><a nAme="idx1073743976"></a><A name="idx1073743977"></A><a naME="idx1073743978"></A><A name="idx1073743979"></A><A NAme="idx1073743980"></a><a NAME="idx1073743981"></a><a naME="idx1073743982"></A><A name="idx1073743983"></a><a name="idx1073743984"></a><a name="idx1073743985"></a><a naMe="idx1073743986"></a><A namE="idx1073743987"></a><a naMe="idx1073743988"></a><a NAME="idx1073743989"></a><a naME="idx1073743990"></A><A name="idx1073743991"></A><A NAme="idx1073743992"></a></p>
<P>Along with those files, it also creates a subdirectory called <TT Class="monofont">/pyds</tt> in which it stores the python extension module (<tt class="monofont">.pyd</tt> extension).</p>
<p>In order to use this extension in your application, the interpreter needs to be able to locate the <tt cLasS="monofont">.pyd</tt> file by looking at the <tT clasS="monofont">sys.path</tt>'s variable.</p>
<P><TT Class="monofont">compile.py</TT> is available at</P>
<P><a tarGET="_blank" Href="http://starship.python.net:9673/crew/da/code/compile">http://starship.python.net:9673/crew/da/Code/compile</a><A NAMe="idx1073743993"></a><a name="idx1073743994"></a><a name="idx1073743995"></a><a name="idx1073743996"></a>
</P>
<h5>Installing and Using Dynamic Modules</h5>
<P>You have four simple choices:</p>
<ul>
<Li><p>Place your <tt ClasS="monofont">module.so</TT> or <Tt claSS="monofont">module.dll</TT> in a directory that is defined by your <tt clASS="monofont">PYTHONPATH</Tt> environment variable. The <tt cLASS="monofont">site-packages</tt> directory under the <tt class="monofont">lib</tt> directory is a good place to put your extension modules.</p>
</li>
<li><p>At runtime, you can add the extension module's path to <tt cLasS="monofont">sys.path.</tt></p>
</Li>
<li><p>On Windows, you can place the extension module in the same directory of the <Tt clASS="monofont">python.exe</Tt> file.</p>
</li>
<LI><P>Put the extension in the current directory when you start Python.</P>
</li>
</ul>
<P>You won't find any difference while running dynamic modules. They act exactly the same way as the static modules that are linked to the interpreter.<A NAme="idx1073743997"></a><a NAME="idx1073743998"></a><a name="idx1073743999"></a><a name="idx1073744000"></a><a name="idx1073744001"></a><a NamE="idx1073744002"></a><a nAme="idx1073744003"></a><a Name="idx1073744004"></A><A NAme="idx1073744005"></a><a NAME="idx1073744006"></a><a naME="idx1073744007"></A><A name="idx1073744008"></A><A NAme="idx1073744009"></a><a name="idx1073744010"></a><a name="idx1073744011"></a><a name="idx1073744012"></A><a nAme="idx1073744013"></a><A name="idx1073744014"></A><a naME="idx1073744015"></A><A name="idx1073744016"></A><A NAme="idx1073744017"></a><a NAME="idx1073744018"></a></p>
<h5>Accessing Generic DLLs</h5>
<P><A NAme="idx1073744019"></a>Sam Rushing has created an extension module called <a name="idx1073744020"></a><a name="idx1073744021"></a><tt claSs="monofont">calldll</tT> that enables Python to call any function that is part of a <tt cLass="monofont">Windows DLL.</tT> It doesn't matter whether the <tt cLASS="monofont">DLL</tt> is a Python extension.</p>
<p>The problem to remember is that <A NAMe="idx1073744022"></a><a nAME="idx1073744023"></A>errors caused by non-Python extension <tt clASS="monofont">DLL</Tt>s don't return exception codes but error messages.</p>
<p>With this module you can call any function in any <tt class="monofont">DLL.</tt> This means that you can do just about anything on Win32. This module includes a library that gives access to lots of the system GUI features, and a 'callback'generator for <tt class="monofont">i386,</tT> which lets external functions call back into Python as if it were C. (Much of the Win32 API uses callbacks.)</p>
<p>Along with that, you can access ODBC by directly calling functions in <Tt clAss="monofont">odbc32.dll</tt> using a wrapper module called <A namE="idx1073744024"></A><A Name="idx1073744025"></a><TT CLass="monofont">odbc.py.</tT> The ODBC module is implemented using <TT Class="monofont">calldll,</TT> and it has a few extra practical pieces; code for managing data sources, installing ODBC itself, and creating and maintaining Jet (Microsoft Access) databases. It has also been tested with ODBC drivers from Oracle and Objectivity. Of course, using <TT class="monofont">calldll</tt> destroys any platform or architecture independence your program may have had.</p>
<p>You can see more details at <a target="_blank" href="http://www.nightmare.com/software.html">http://www.nightmare.com/software.html</A>.<a nAme="idx1073744026"></a><A name="idx1073744027"></A><a naME="idx1073744028"></A><A name="idx1073744029"></A><A NAme="idx1073744030"></a><a NAME="idx1073744031"></a><a naME="idx1073744032"></A><A name="idx1073744033"></a><a name="idx1073744034"></a><a name="idx1073744035"></a><a naMe="idx1073744036"></a><A namE="idx1073744037"></a><a naMe="idx1073744038"></a><a NAME="idx1073744039"></a><a naME="idx1073744040"></A><A name="idx1073744041"></A></P>
</FOnt>
<P><TABLE width="100%" border=0><TR valign="top"><TD><font size=1 color="#C0C0C0"><br></font></TD><TD align=right><font size=1 color="#C0C0C0">Last updated on 1/30/2002<br>Python Developer's Handbook, © 2002 Sams Publishing</font></TD></TR></TABLE></P>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="121.html" title="Extending"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=122" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="122.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="123.html" title="SWIG桾he Simple Wrapper Interface Generator"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
</TD></TR></TABLE>
<br><TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD><H4 class=Title>Index terms contained in this section</H4>
<font size=2>
<a href="#idx1073743912">*shared* flag</a><BR>
<a href="#idx1073743910">*static* flag</a><BR>
applications<BR>
non-Python<BR>
<a href="#idx1073743898">embedding Python objects in;compiling and linking extension modules</a> <a href="#idx1073743922">2nd</a> <a href="#idx1073743940">3rd</a> <a href="#idx1073743959">4th</a> <a href="#idx1073743981">5th</a> <a href="#idx1073744004">6th</a> <a href="#idx1073744033">7th</a><BR>
<a href="#idx1073743970">Ascher, David</a><BR>
C programming language<BR>
<a href="#idx1073743901">extending and embedding Python</a> <a href="#idx1073743925">2nd</a> <a href="#idx1073743943">3rd</a> <a href="#idx1073743962">4th</a> <a href="#idx1073743984">5th</a> <a href="#idx1073744007">6th</a><BR>
C++ programming language<BR>
<a href="#idx1073743903">extending and embedding Python</a> <a href="#idx1073743927">2nd</a> <a href="#idx1073743945">3rd</a> <a href="#idx1073743964">4th</a> <a href="#idx1073743986">5th</a> <a href="#idx1073744009">6th</a><BR>
<a href="#idx1073744020">calldll module</a> <a href="#idx1073744040">2nd</a><BR>
<a href="#idx1073743971">compile.py tool</a> <a href="#idx1073743994">2nd</a><BR>
compiling<BR>
<a href="#idx1073743893">extension modules</a> <a href="#idx1073743917">2nd</a> <a href="#idx1073743935">3rd</a> <a href="#idx1073743954">4th</a> <a href="#idx1073743976">5th</a> <a href="#idx1073743999">6th</a> <a href="#idx1073744028">7th</a><BR>
creating<BR>
Python extension modules<BR>
<a href="#idx1073743891">compiling and linking</a> <a href="#idx1073743915">2nd</a> <a href="#idx1073743933">3rd</a> <a href="#idx1073743952">4th</a> <a href="#idx1073743974">5th</a> <a href="#idx1073743997">6th</a> <a href="#idx1073744026">7th</a><BR>
dynamic extensions<BR>
<a href="#idx1073743967">linking to interpreters</a> <a href="#idx1073743989">2nd</a> <a href="#idx1073744012">3rd</a> <a href="#idx1073744037">4th</a><BR>
dynamic modules<BR>
<a href="#idx1073744016">installing and running</a><BR>
embedding<BR>
Python objects<BR>
<a href="#idx1073743892">compiling and linking extension modules</a> <a href="#idx1073743916">2nd</a> <a href="#idx1073743934">3rd</a> <a href="#idx1073743953">4th</a> <a href="#idx1073743975">5th</a> <a href="#idx1073743998">6th</a> <a href="#idx1073744027">7th</a><BR>
error messages<BR>
<a href="#idx1073744022">non-Python extension dynamic link libraries (DLLs)</a><BR>
extension modules<BR>
<a href="#idx1073743895">compiling and linking</a> <a href="#idx1073743919">2nd</a> <a href="#idx1073743937">3rd</a> <a href="#idx1073743956">4th</a> <a href="#idx1073743978">5th</a> <a href="#idx1073744001">6th</a> <a href="#idx1073744030">7th</a><BR>
extensions<BR>
dynamic<BR>
<a href="#idx1073743968">linking to interpreters</a> <a href="#idx1073743990">2nd</a> <a href="#idx1073744013">3rd</a> <a href="#idx1073744038">4th</a><BR>
static<BR>
<a href="#idx1073743907">linking to interpreters</a> <a href="#idx1073743931">2nd</a> <a href="#idx1073743949">3rd</a><BR>
flags<BR>
<a href="#idx1073743913">*shared*</a><BR>
<a href="#idx1073743911">*static*</a><BR>
installing<BR>
<a href="#idx1073744015">dynamic modules</a><BR>
interpreters<BR>
<a href="#idx1073743969">linking dynamic extensions</a> <a href="#idx1073743991">2nd</a> <a href="#idx1073744014">3rd</a> <a href="#idx1073744039">4th</a><BR>
<a href="#idx1073743908">linking static extensions</a> <a href="#idx1073743932">2nd</a> <a href="#idx1073743950">3rd</a><BR>
linking<BR>
<a href="#idx1073743966">dynamic extensions to interpreters</a> <a href="#idx1073743988">2nd</a> <a href="#idx1073744011">3rd</a> <a href="#idx1073744036">4th</a><BR>
<a href="#idx1073743894">extension modules</a> <a href="#idx1073743918">2nd</a> <a href="#idx1073743936">3rd</a> <a href="#idx1073743955">4th</a> <a href="#idx1073743977">5th</a> <a href="#idx1073744000">6th</a> <a href="#idx1073744029">7th</a><BR>
<a href="#idx1073743905">static extensions to interpreters</a> <a href="#idx1073743929">2nd</a> <a href="#idx1073743947">3rd</a><BR>
messages<BR>
error<BR>
<a href="#idx1073744023">non-Python extension dynamic link libraries (DLLs)</a><BR>
modules<BR>
<a href="#idx1073744021">calldll</a> <a href="#idx1073744041">2nd</a><BR>
dynamic<BR>
<a href="#idx1073744017">installing and running</a><BR>
extension<BR>
<a href="#idx1073743896">compiling and linking</a> <a href="#idx1073743920">2nd</a> <a href="#idx1073743938">3rd</a> <a href="#idx1073743957">4th</a> <a href="#idx1073743979">5th</a> <a href="#idx1073744002">6th</a> <a href="#idx1073744031">7th</a><BR>
<a href="#idx1073744025">odbc.py</a><BR>
objects<BR>
Python<BR>
<a href="#idx1073743897">embedding in non-Python applications;compiling and linking extension modules</a> <a href="#idx1073743921">2nd</a> <a href="#idx1073743939">3rd</a> <a href="#idx1073743958">4th</a> <a href="#idx1073743980">5th</a> <a href="#idx1073744003">6th</a> <a href="#idx1073744032">7th</a><BR>
<a href="#idx1073744024">odbc.py module</a><BR>
programming languages<BR>
C<BR>
<a href="#idx1073743902">extending and embedding Python</a> <a href="#idx1073743926">2nd</a> <a href="#idx1073743944">3rd</a> <a href="#idx1073743963">4th</a> <a href="#idx1073743985">5th</a> <a href="#idx1073744008">6th</a><BR>
C++<BR>
<a href="#idx1073743904">extending and embedding Python</a> <a href="#idx1073743928">2nd</a> <a href="#idx1073743946">3rd</a> <a href="#idx1073743965">4th</a> <a href="#idx1073743987">5th</a> <a href="#idx1073744010">6th</a><BR>
programs<BR>
non-Python<BR>
<a href="#idx1073743899">embedding Python objects in;compiling and linking extension modules</a> <a href="#idx1073743923">2nd</a> <a href="#idx1073743941">3rd</a> <a href="#idx1073743960">4th</a> <a href="#idx1073743982">5th</a> <a href="#idx1073744005">6th</a> <a href="#idx1073744034">7th</a><BR>
running<BR>
<a href="#idx1073744018">dynamic modules</a><BR>
<a href="#idx1073744019">Rushing, Sam</a><BR>
software<BR>
non-Python<BR>
<a href="#idx1073743900">embedding Python objects in;compiling and linking extension modules</a> <a href="#idx1073743924">2nd</a> <a href="#idx1073743942">3rd</a> <a href="#idx1073743961">4th</a> <a href="#idx1073743983">5th</a> <a href="#idx1073744006">6th</a> <a href="#idx1073744035">7th</a><BR>
static extensions<BR>
<a href="#idx1073743906">linking to interpreters</a> <a href="#idx1073743930">2nd</a> <a href="#idx1073743948">3rd</a><BR>
tools<BR>
<a href="#idx1073743972">compile.py</a> <a href="#idx1073743995">2nd</a><BR>
UNIX<BR>
<a href="#idx1073743909">linking static extensions to interpreters</a> <a href="#idx1073743914">2nd</a><BR>
utilities<BR>
<a href="#idx1073743973">compile.py</a> <a href="#idx1073743996">2nd</a><BR>
Windows<BR>
<a href="#idx1073743992">linking dynamic extensions to interpreters</a> <a href="#idx1073743993">2nd</a><BR>
<a href="#idx1073743951">linking static extensions to interpreters</a><BR>
<BR>
</font></TD></TR></TABLE>
<!--EndOfBrowse-->
</TD></TR></TABLE>
<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#990000><tr><td><p align=center><font size=1 face="verdana,arial,helvetica" color=white>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -