【问题标题】:Facing problem in running this Python code运行此 Python 代码时面临问题
【发布时间】:2019-12-24 14:43:15
【问题描述】:

我对 Python 很陌生。安装包 cosmoTransitions 后,我必须运行代码,并且有一些示例程序,我已经尝试过一个并收到下面提到的错误,但不明白错误来自哪里

def approxZeroTMin(self):
    # There are generically two minima at zero temperature in this model,
    # and we want to include both of them.
    v = v2**.5
    return [np.array([v,v,v]), np.array([v,v,v])]


def makePlots(m=None):
import matplotlib.pyplot as plt
if m is None:
    m = model1()
    m.findAllTransitions()
# --
plt.figure()
m.plotPhasesPhi()
plt.axis([0,300,-50,550])
plt.title("Minima as a function of temperature")
plt.show()
# --
plt.figure(figsize=(8,3))
ax = plt.subplot(131)
T = 0
m.plot2d((-450,450,-450,450), T=T, cfrac=.4,clevs=65,n=100,lw=.5)
ax.set_aspect('equal')
ax.set_title("$T = %0.2f$" % T)
ax.set_xlabel(R"$\phi_1$")
ax.set_ylabel(R"$\phi_2$")
ax = plt.subplot(132)
T = m.TnTrans[1]['Tnuc']
instanton = m.TnTrans[1]['instanton']
phi = instanton.Phi
m.plot2d((-450,450,-450,450), T=T, cfrac=.4,clevs=65,n=100,lw=.5)
ax.plot(phi[:,0], phi[:,1], 'k')
ax.set_aspect('equal')
ax.set_title("$T = %0.2f$" % T)
ax.set_yticklabels([])
ax.set_xlabel(R"$\phi_1$")
ax = plt.subplot(133)
T = m.TnTrans[0]['Tnuc']
m.plot2d((-450,450,-450,450), T=T, cfrac=.4,clevs=65,n=100,lw=.5)
ax.set_aspect('equal')
ax.set_title("$T = %0.2f$" % T)
ax.set_yticklabels([])
ax.set_xlabel(R"$\phi_1$")
# --
plt.figure()
plt.plot(instanton.profile1D.R, instanton.profile1D.Phi)
plt.xlabel("radius")
plt.ylabel(R"$\phi-\phi_{min}$ (along the path)")
plt.title("Tunneling profile")

但是在运行它之后它显示了太多错误,我也无法区分它们来自哪里。 这是错误消息:

Traceback (most recent call last):
File "C:/Users/SATYABRATA/AppData/Local/Programs/Python/Python38-32  /sdd.py", line 2, in <module>
from cosmoTransitions import generic_potential
File "C:\Users\SATYABRATA\AppData\Local\Programs\Python\Python38-32    \lib\site-packages\cosmoTransitions\generic_potential.py", line 20, in <module>
from .finiteT import Jb_spline as Jb
File "C:\Users\SATYABRATA\AppData\Local\Programs\Python\Python38-32\lib\site-packages\cosmoTransitions\finiteT.py", line 25, in <module>
from scipy.misc import factorial as fac
ImportError: cannot import name 'factorial' from 'scipy.misc' (C:\Users     \SATYABRATA\AppData\Local\Programs\Python\Python38-32\lib\site-packages\scipy\misc\__init__.py)

【问题讨论】:

  • Please don't post screenshots of text。它们无法被搜索或复制,并且可用性差。相反,将代码作为文本直接粘贴到您的问题中。如果选择它并单击{} 按钮或Ctrl+K,则代码块将缩进四个空格,这将导致其呈现为代码。
  • 你安装了 scipy 吗?
  • 是的 scipy、numpy、matplotlib、cosmoTransitions 全部安装
  • docs.scipy.org/doc/scipy-1.1.0/reference/generated/… 说 scipy.misc.factorial 已被贬低。如果您已经拥有最新版本的 cosmoTransitions(可能尝试查找开发版本),您可以尝试将 scipy 替换为旧版本。或者尝试更改源以用 scipy.special.factorial 替换对 scipy.misc.factorial 的调用
  • 在 shell 中尝试from scipy.special import factorial,如果可行,更改finiteT.py 中的那一行并向维护者提供反馈。

标签: python package


【解决方案1】:

在最新版本的 cosmoTransition 包中,有这行 from scipy.misc import factorial as fac 导致了这个问题。您的 scipy 版本可能高于 1.0.0。因为他们反对这个。您应该尝试使用以下命令将 scipy 版本降级到 1.0.0:python3 -m pip install scipy==1.0 --upgrade。另外,据我了解this post,您也可以尝试使用1.2

【讨论】:

  • 其实在 scipy.special 中。 --> from scipy.special import factorial
  • 是的,你是对的,但它在 scipy 文档页面中被声明为已弃用。所以...
  • the docs 中的实际声明是从scipy.misc 导入已被弃用(不是函数本身)并从scipy.special 导入。
【解决方案2】:

在 python shell 中尝试

from scipy.special import factorial

如果您没有收到 ImportError,请在您的编辑器中打开 finiteT.py(来自您发布的错误消息的文件路径):

"C:\Users\SATYABRATA\AppData\Local\Programs\Python\Python38-32\lib\site-packages\cosmoTransitions\finiteT.py"

第25行更改

from scipy.misc import factorial as fac

from scipy.special import factorial as fac

try: 
    from scipy.misc import factorial as fac 
except ImportError: 
    from scipy.special import factorial as fac 

自 2018 年 2 月以来,该存储库没有任何提交 - 所以您不妨更改您的副本。

【讨论】:

  • 是你吗?我没有检查。 Gj.
猜你喜欢
  • 2022-07-04
  • 2017-02-07
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 2019-06-27
  • 1970-01-01
  • 2011-04-05
相关资源
最近更新 更多