Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Python Interpreters used in the same project?

Tags:

python

pycharm

I am using pycharm and it only lets you use one interpreter for example Python 2.7.5 or Python 3.5.1.

The problem is I have certain modules one from google to access analytics data and one called docxfactory that I want to use together.

I can only get docxfactory to work on Python 3.5.1 and I can only get analytics to work on 2.7.5...

How can I get it so I can use these two modules together? I read an answer on here that said to have them in two different projects and unless I did that wrong I tried that with no success... any ideas?

like image 892
McHenry Avatar asked Jun 01 '16 20:06

McHenry


People also ask

How does Python 3 interpreter work?

In the terminal type the command "python3" ("python" on Windows, or sometimes "py"). This runs the interpreter program directly. On the Mac type ctrl-d to exit (on Windows ctrl-z).


2 Answers

This is actually possible with a bit of hacking.

  • close the pycharm IDE.
  • open $PROJECT/.idea/modules.xml and add another module with a different name like this:

<modules> <module fileurl="file://$PROJECT_DIR$/.idea/jobs.iml" filepath="$PROJECT_DIR$/.idea/jobs.iml" /> <module fileurl="file://$PROJECT_DIR$/.idea/synonymer.iml" filepath="$PROJECT_DIR$/.idea/synonymer.iml" /> </modules>

  • now add a file by the same name in $PROJECT/.idea.
  • reopen the project.
  • open project settings where you will see something like this:enter image description here
  • notice that now there are two modules and you can configure each one separately. This means that you can configure folders, exclusion and different interpreters. Now it's you job to configure things properly.

Caveat: This is a hack which has no GUI in pycharm. This could stop working at any upgrade to pycharm. However, I don't think it will for various reasons.

like image 177
Mark Veltzer Avatar answered Oct 10 '22 12:10

Mark Veltzer


Not sure if this applies in Community Edition, but in Professional, this is straightforward. We use it to have a separately managed virtualenv + interpreter for each of several Google Cloud Functions under the same Git + PyCharm projects.

Assuming you have a project structure like mine:

myproject
│   ├── function1
│   │   ├── requirements.txt
│   │   └── main.py
│   └── function2
│       ├── requirements.txt
│       └── main.py
├── README.md
  1. Open your project and set up the interpreter + virtualenv as usual (File -> Settings -> Project -> Project Interpreter). Create a new virtualenv interpreter, saving it under your project's root (e.g., myproject/venv)
    • Note: This will be the default interpreter which we will override for function1 and function2.
  2. Create a new PyCharm project for each subfolder that needs its own virtualenv. You'll attach this project to your existing project.
    • File -> Open -> Select the subfolder (e.g., function1) -> OK -> "Attach"
    • Note: A bug in PyCharm may cause an error message here... if so, choose open in a new window instead of attach, then close the new window and try again.
  3. Go back to project interpreter settings. Notice: there are now two projects listed, the root myproject and the subfolder function1.
    • Configure each project's interpreter as you like, e.g., creating a new virtualenv interpreter stored under myproject/function1/venv. These now act as totally independent interpreters and PyCharm plays nicely with both.
  4. Repeat steps 2-3 for as many subfolders/virtualenvs as you want, such as function2 in my example.

If everything went well, you'll notice that the subfolders are bolded, indicating that they are really separate projects.

like image 16
Jake Biesinger Avatar answered Oct 10 '22 12:10

Jake Biesinger