我有一个使用 panda 和 sklern 的脚本 gapminder1.py。
# TODO: Add import statements
import pandas as pd
from sklearn.linear_model import LinearRegression
# Assign the dataframe to this variable.
# TODO: Load the data
bmi_life_data = pd.read_csv("CSV_DATA/bmi_and_life_expectancy.csv")
print(bmi_life_data)
# Make and fit the linear regression model
#TODO: Fit the model and Assign it to bmi_life_model
bmi_life_model = LinearRegression()
bmi_life_model.fit(bmi_life_data[['BMI']], bmi_life_data[['Life expectancy']])
# Make a prediction using the model
# TODO: Predict life expectancy for a BMI value of 21.07931
laos_life_exp = bmi_life_model.predict(21.07931)
我正在从 cmd 控制台运行脚本,它工作正常,但来自 pycharm 的相同脚本向我显示错误
C:\Users\tripathi\AppData\Local\Continuum\anaconda3\envs\dsnd\python.exe C:/Users/tripathi/PycharmProjects/dsnd/gapminder1.py
Traceback (most recent call last):
File "C:/Users/tripathi/PycharmProjects/dsnd/gapminder1.py", line 3, in <module>
import pandas as pd
File "C:\Users\tripathi\AppData\Local\Continuum\anaconda3\envs\dsnd\lib\site-packages\pandas\__init__.py", line 19, in <module>
"Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']
我在两个地方都使用相同的 conda 环境,但不确定为什么它不能正常工作。
最佳答案
至少在 Windows 上的 PyCharm 中这是一个已知问题。 PyCharm 使用了 conda 环境但实际上并未激活它,因此未加载 env 的环境变量。这个问题已经有一段时间了,看起来很容易修复,但由于某种原因他们还没有修复它。
唯一的解决方法是从激活环境的 cmd 窗口启动 PyCharm,或者可能在作为外部工具执行之前运行环境激活。
关于python - conda 虚拟环境不适用于 pycharm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51455571/