简体   繁体   English

导入模块时,如何指定解释器来编译模块?

[英]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.

相关问题 导入python模块时如何解决KeyError? - How do I solve a KeyError when importing a python module? 导入模块,但解释器无法识别该模块python中的功能 - Importing a module but the interpreter is not recognizing the function in that module python 导入模块在 Jupyter 笔记本和解释器中的行为不同? - importing module behaves differently in Jupyter notebook and Interpreter? 导入在解释器启动时不可用的Python模块 - Importing a Python module that was not available at interpreter startup 导入Python模块只能在解释器中运行,而不能在脚本中运行 - Importing Python module works in interpreter but not script 在Python中导入RTree时,如何解决“ ImportError:没有名为索引的模块”? - How do I fix “ImportError: No module named index” when importing RTree in Python? 导入模块时如何避免导入模块依赖性? - How to avoid importing module dependencies when importing module? 尝试通过解释器运行Python脚本时,为什么会出现“ ImportError:未命名模块”的提示? - Why do I get “ImportError: No module named” when I try to run my Python script via the Interpreter? 如何在python中指定根模块目录? - How do I specify the root module directory in python? 如何在 atom 上指定模块(django)的版本? - How do i specify the version of a module(django) on atom?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM