[英]When importing a module, how do I specify an interpreter to compile the module?
I wrote a module (mymodule32.py) that contains a library that only supports 32 bit Python. 我编写了一个模块(mymodule32.py),其中包含仅支持32位Python的库。 I want to import this module in a 64 bit Python file (main64.py).
我想将此模块导入64位Python文件(main64.py)。 But after searching, I realized that if I import the module in 64 bit Python, it will be compiled with the 64 bit interpreter.
但是搜索之后,我意识到,如果我以64位Python导入模块,则将使用64位解释器对其进行编译。 I will import mymodule32.py in main64.py, but I want to interpret it with a 32 bit interpreter.
我将在main64.py中导入mymodule32.py,但我想使用32位解释器对其进行解释。
I installed Python 3.5 64 bit, and in addition I installed Python 3.5 32 bit under the name py35_32 in the Anaconda virtual environment. 我安装了Python 3.5 64位,并且在Anaconda虚拟环境中以py35_32的名称安装了Python 3.5 32位。 I wrote the pseudo code below I wanted(it does not work, of course).
我在想要的下面写了伪代码(当然不起作用)。
import(interpreter = py35_32) mymodule32
You can't do what you want with import
. 您无法使用
import
做任何事情。 The interpreter is a single process, and can't switch between 64-bit and 32-bit modes. 解释器是单个进程,不能在64位和32位模式之间切换。
Your options are to 您的选择是
Run all your code in a Python 32-bit binary. 在Python 32位二进制文件中运行所有代码。 You can guard against using a 64-bit binary by testing for
sys.maxsize == (2 ** 31 - 1)
. 您可以通过测试
sys.maxsize == (2 ** 31 - 1)
来防止使用64位二进制文件。
Make your library work correctly on a 64-bit interpreter too (just make sure your 32-bit arithmetic is properly masked). 也使您的库在64位解释器上也能正常工作(只需确保正确屏蔽了32位算术)。
Run another Python process as a subprocess; 运行另一个Python进程作为子进程; that child process would import your module and use some form of IPC (interprocess communication) to pass data and results between the parent 64-bit process and the child 32-bit interpreter.
该子进程将导入您的模块,并使用某种形式的IPC(进程间通信)在父64位进程和子32位解释器之间传递数据和结果。 There are no ready-made solutions here, however.
但是,这里没有现成的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.