如何从 scipy.constants.physical_constants 导入物理常数?

问题描述

我正在尝试从 electron volt-joule relationship 导入 scipy.constants.physical_constants 以用于数值物理问题。这可能是一个非常简单的问题,或者是对physical_constants字典的误解,但在谷歌搜索了2小时后我仍然不知所措。

我试过了

from scipy.constants.physical_constants import electron volt_joule relationship

我也试过

import scipy.constants.physical_constants["electron volt-joule relationship"]

产生什么

File "<ipython-input-22-7c2fb3ec2156>",line 3 import scipy.constants.physical_constants["electron volt-joule relationship"] ^ SyntaxError: invalid Syntax

我是否误解了这些物理常数的使用?从 scipy.org 文档中,我看到它们的形式是 physical_constants[name] = (value,unit,uncertainty)

这样我就能得到

print(scipy.constants.physical_constants["electron volt-joule relationship"])

返回

(1.602176634e-19,'J',0.0)

甚至

import scipy.constants.physical_constants

返回错误

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-21-b4d34ca28080> in <module> 
----> 1 import scipy.constants.physical_constants

ModuleNotFoundError: No module named 'scipy.constants.physical_constants'

这个常量库是否充满了您可以引用的值、单位和不确定性的值,但实际上并未在计算中使用?

解决方法

看起来原始海报的导入语句格式不正确。 要访问常量,请包含以下导入语句:

from scipy import constants

然后要访问特定的常量,请尝试:

print(constants.electron_volt)

returns:
1.602176634e-19

如果没有找到scipy包,可以添加:

pip install scipy