Can I detect if my code is running on cPython or Jython?

喜你入骨 提交于 2019-12-28 06:59:47

问题


I'm working on a small django project that will be deployed in a servlet container later. But development is much faster if I work with cPython instead of Jython. So what I want to do is test if my code is running on cPython or Jython in my settiings.py so I can tell it to use the appropriate db driver (postgresql_psycopg2 or doj.backends.zxjdbc.postgresql). Is there a simple way to do this?


回答1:


if you're running Jython

import platform
platform.system()  

return 'Java'
here has some discussion, hope this helps.




回答2:


The most clear-cut way is:

import platform

platform.python_implementation()

'CPython'

By default, most of the time the underlying interpreter is CPython only which is also arguably the most efficient one :)




回答3:


As sunqiang pointed out

import platform
platform.system()

works for Jython 2.5, but this doesn't work on Jython 2.2 (the previous Jython release). Also, there has been some discussion about returning more operating system specific details for calls like these in Jython 3.x. Nothing has been decided there, but to be safely backwards and forwards compatible, I would suggest using:

import sys
sys.platform.startswith('java')

Which will return True for Jython and False everywhere else (actually in Jython 2.2 or older it returns 1 for Jython and 0 everywhere else, but this will still work fine in if statements and other checks). This call works in Jython at least as far back as 2.1, and will work for the foreseeable future.

In Python versions 2.6 or above (note Jython 2.6 has not yet been released) another option is:

import platform
platform.python_implementation

Which returns 'CPython' for the C implementation of Python, 'IronPython' for IronPython and will return 'Jython' for Jython. Obviously this one isn't backwards compatible below 2.6, but will be forwards compatible.




回答4:


You'll have unique settings.py for every different environment.

Your development settings.py should not be your QA/Test or production settings.py.

What we do is this.

We have a "master" settings.py that contains the installed apps and other items which don't change much.

We have environment-specific files with names like settings_dev_win32.py and settings_qa_linux2.py and 'settings_co_linux2.py`, etc.

Each of these environment-specific settings imports the "master" settings, and then overrides things like the DB driver. Since each settings file is unique to an environment, there are no if-statements and no detecting which environment we're running in.

Production (in Apache, using mod_wsgi and mysql) uses the settings_prod_linux2.py file and no other.

Development (in Windows using sqlite) uses the settings_dev_win32.py file.




回答5:


In Python 3.3 and beyond you can use sys.implementation and look at the name attribute.



来源:https://stackoverflow.com/questions/1103487/can-i-detect-if-my-code-is-running-on-cpython-or-jython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!