2

The problem I am facing is similar to this: PyCharm not using my configured virtualenv interpreter

I am trying to run a python module in PyCharm in a virtual environment. I have set up the correct Project Interpreter and the Python interpreter to point to the VirtualEnv interpreter. Have I set it up incorrectly?

Python Interpreter:

enter image description here Project Interpreter:

enter image description here When I run I get this message:

/projects/DE/.env/bin/python -m training
Could not import runpy module
Traceback (most recent call last):
  File "/Users/deepak/anaconda/lib/python3.6/runpy.py", line 14, in <module>
    import importlib.machinery # importlib first so we can test #15386 via -m
  File "/projects/DE/.env/lib/python3.6/importlib/__init__.py", line 57, in <module>
    import types
  File "/projects/DE/training/types.py", line 2, in <module>
import json
  File "/Users/deepak/anaconda/lib/python3.6/json/__init__.py", line 106, in <module>
from .decoder import JSONDecoder, JSONDecodeError
  File "/Users/deepak/anaconda/lib/python3.6/json/decoder.py", line 3, in <module>
import re
  File "/projects/DE/.env/lib/python3.6/re.py", line 122, in <module>
import enum
  File "/projects/DE/.env/lib/python3.6/enum.py", line 2, in <module>
from types import MappingProxyType, DynamicClassAttribute
ImportError: cannot import name 'MappingProxyType'```

I am not sure why PyCharm is using the Anaconda python. I am not sure if there are any other places where the interpreter needs to be changed to point to the virtualenv.

The same command (/projects/DE/.env/bin/python -m training) seems to work when I run from the terminal.

What change can I make so that I can run the module from PyCharm?

Nipun Thennakoon
  • 3,586
  • 1
  • 18
  • 26
Dualphase
  • 29
  • 1
  • 4
  • Did you make sure you applied the changes? – ds_secret Jul 31 '18 at 21:39
  • yes, I did apply the changes in the project and python interpreter. – Dualphase Jul 31 '18 at 21:43
  • What command did you enter in terminal and what OS are you in? – ds_secret Jul 31 '18 at 21:49
  • In the upper right hand corner of PyCharm it will show `[file name].py`, click on that. It will pop open a window whose title is `Run/Debug Configurations`. In the Configuration tab, it should have a section called `Python Interpreter`. Is it using Anaconda, Python, or your virtual environment? – ds_secret Jul 31 '18 at 21:57
  • The same command that PyCharm runs (```/projects/DE/.env/bin/python -m training ```). This is the same as the first line of the stackstrace I posted above. I am on Mac High Sierra. – Dualphase Jul 31 '18 at 21:57
  • It is the virtual env interpreter in- `Python Interpreter` in the Edit Configurations. – Dualphase Jul 31 '18 at 22:00
  • In terminal run the following commands: `cd venv/bin`, `source activate`, cd ..`, `cd ..`, and finally `python [file name].py`. Does your program run properly? – ds_secret Jul 31 '18 at 22:03
  • yes sir that seems to work. This seems to be something wrong in how I am configuring pycharm. – Dualphase Jul 31 '18 at 22:06

1 Answers1

1

In Pycharm version 2017.3, you can go to Pycharm -> Preferences -> Project -> Project Interpreter -> -> settings button on the right of the python interpreter text box -> Add local

This will open a new window with virtualenv Environment as one of the options on the left. On Selecting it, you will get an option to make a new virtualenv environment or use an existing virtual environment. Here next to the dropdown text box, you can click "..." and browse to your existing virtualenv created using pyenv and select it. It will select this virtualenv when you start terminal from Pycharm and also use the corresponding python interpreter as set while creating that virtualenv.

enter image description here

Viral Modi
  • 1,957
  • 1
  • 9
  • 18