Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set different interpreters for specific files in PyCharm

I'm working on a Django project that is using Python 3 in a virtualenv. I just came across fabric, which only works under Python 2, so I installed it system wide instead of in my virtualenv (is it even possible to put this in my Python 3 virtualenv, btw?).

The problem here is that I've set PyCharm to use Python 3 as interpreter and having fabric installed for Python 2. When I edit my fabric file it says that all imports from fabric are unknown.

Is there any way I can solve this? Any way to assign my fabric file to use the Python 2 interpreter instead of Python 3, or some other solution?

like image 362
Christoffer Karlsson Avatar asked Jul 03 '15 18:07

Christoffer Karlsson


1 Answers

This is possible to do (at least with the current PyCharm 2018.2.4), but it takes some manual effort and cannot be done through the GUI.

  1. Exit PyCharm
  2. Navigate to the .idea folder of your project
  3. Edit modules.xml
    • Duplicate the <module> line and change the fileurl and filepath attributes. Mine looked like this when I was done: <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="ProjectModuleManager"> <modules> <module fileurl="file://$PROJECT_DIR$/.idea/dataops.iml" filepath="$PROJECT_DIR$/.idea/dataops.iml" /> <module fileurl="file://$PROJECT_DIR$/.idea/dataops_py27.iml" filepath="$PROJECT_DIR$/.idea/dataops_py27.iml" /> </modules> </component> </project>
  4. Copy $PROJECT_NAME.iml to the name you gave your new module. In my case I did: cp dataops.iml dataops_py27.iml

  5. Open your project back up in PyCharm and go to Preferences > Project > Project Interpreter. You will see the two modules (the initial module and the new one you just created). Select the new one and configure the interpreter by clicking the gear icon in the upper right corner of the window and selecting Add...

  6. Go to Preferences > Project > Project Structure. Make sure your new module is selected, remove the old content root and add a new one.

Note: If you have many files in the folder and do not want the newly added interpreter to apply to all of them you can exclude them in the Exclude files: text box located at the bottom of the Preferences > Project > Project Structure setting.

like image 173
Mike Furlender Avatar answered Oct 14 '22 02:10

Mike Furlender