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?

8
  • Did you make sure you applied the changes? Commented Jul 31, 2018 at 21:39
  • yes, I did apply the changes in the project and python interpreter. Commented Jul 31, 2018 at 21:43
  • What command did you enter in terminal and what OS are you in? Commented Jul 31, 2018 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? Commented Jul 31, 2018 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. Commented Jul 31, 2018 at 21:57

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.