Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conda virtual environment not working with pycharm

i have a script gapminder1.py which uses the panda and sklern.

# 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)

i am running the script from cmd console which is working fine but the same script from pycharm showing me the error

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']

i am using same conda environment on both the places but not sure why it is not working fine.

like image 407
om tripathi Avatar asked Jul 21 '18 11:07

om tripathi


5 Answers

This is a known issue in PyCharm on Windows at least. The conda environment is used but not actually activated by PyCharm so environment variables for the env are not loaded. It's been a problem for a while, seems it'd be easy to fix but for some reason they haven't fixed it.

The only work around is to start PyCharm from a cmd window in which the env is activated, or possibly run the environment activation prior to execution as an external tool.

like image 114
Faraz Avatar answered Nov 17 '22 10:11

Faraz


Okay, I think it's an issue where Conda and pycharm are not communicating with each other properly. This is the reason why I always create my virtual environment using PyCharm.

Option 1: Create a new virtual environment using PyCharm

Try the instructions by jetbrains.

Option 2: Delete and reconnect to your old Conda environment and check the settings

Perhaps you didn't check some of the boxes when connecting to the environment via PyCharm:

  • Inherit global site-packages
  • Make available to all projects
like image 23
pajamas Avatar answered Nov 17 '22 09:11

pajamas


You have to install numpy into the conda virtual environment using pycharm settings.

  1. Go to Settings->Project->Project Interpreter
  2. Click the green plus mark(Install) as shown below.

enter image description here 3. Search and select numpy from the available packages list and click install package.

enter image description here

like image 2
Nipun Sampath Avatar answered Nov 17 '22 08:11

Nipun Sampath


Make sure you have pip installed numpy, also check on the paths at environment variables at My Computer or Control Panel.

like image 1
brahWasHere Avatar answered Nov 17 '22 08:11

brahWasHere


Method 1

Try uninstaling and again installing pandas and numpy by:

conda uninstall pandas
conda uninstall numpy
conda install pandas
conda install numpy

You have to do this using the same python(conda) environment which is in the question

Method 2

Try deleting all .pyc files in the project directory

Method 3

Try importing numpy in your gapminder1.py by adding a import numpy line at the top

You might need to use multiple methods. Follow the solution according to the given order.

Hope it helps. Thanks and regards

like image 1
Agile_Eagle Avatar answered Nov 17 '22 09:11

Agile_Eagle