22

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?

7
  • 1
    You can't. There's not a way to use multiple versions of Python in the same project. Commented Jun 1, 2016 at 20:04
  • 1
    There are lots of python libraries for docx files. Do you have to use docxfactory? Commented Jun 1, 2016 at 20:11
  • Also which library are you using from Google? It looks to me like they support both python versions: developers.google.com/api-client-library/python/apis/analytics/… Commented Jun 1, 2016 at 20:20
  • Well on that link for the analytics it says it is only compatible with Python 2.6, 2.7, 3.3, or 3.4... I am using 3.5.1... I do not really know if that means it is not supported or not. Commented Jun 1, 2016 at 20:56
  • Alex, also the docxfactory does exactly what I want in that it creates charts and tables quite easily... I have not found any other library that can do what it does. Not that it does not exist obviously. Commented Jun 1, 2016 at 20:58

4 Answers 4

30

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.

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

6 Comments

Awesome! I got hung up on the initial error during the "Attach" step, but this is a nice workaround.
Thanxasalotl! Works fine in 2021.3 Professional
Step 2 should include a note that a modal pops up mentioning that the directory is not empty. You want to choose the "create from existing sources" option, close the window for the new (sub)project and then in the main project use File -> Attach Project. Navigate to folder and open that.
For step 2, I also had to delete the new project's .idea folder and restart Pycharm to skip the error.
This works with the community edition
|
24

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.

4 Comments

there's a space missing at '... could stop workingat any...'
Awesome. All the other posts say there's no way to do it, but there is very much a way to do it :D
It should also be noted that there is a GUI way to do it … if you have access to IntelliJ IDEA Ultimate (with the Python plugin). In IDEA Ultimate, just pick the menu option "File → Project Structure…", and then you can add modules to the project. These modules remain available and configurable once you switch back to PyCharm.
Better answer is stackoverflow.com/a/56875333/2761682 -- no hacking necessary
9

One idea is to write two seperate scripts; one for analytics and one for docxfactory. Decide which of these is the driver, and have that driver shell the other -- being sure to invoke the appropriate version of python.

ex:

#/usr/bin/env python2.7
import subprocess
# Do something with analytics
# ...
# Now call docxfactory script
subprocess.call(['python3', 'docxcript.py'])

Comments

4

To use different interpreters on pycharm is easy, follow these steps bellow:

  • First you need to add a new interpreter, go to section "settings"

enter image description here

  • Select the tab "Project Interpreter"

enter image description here

  • Click on gear at right side on top

enter image description here

  • Click on Plus signal to add a new interpreter

enter image description here

  • Select the Python Executable interpreter and apply/quit other frames

enter image description here

To change the interpreter, you need to follow this another steps

  • Click on the run listbox, and select the option "Edit Configurations"

enter image description here

  • At this window locate the section "Python Interpreter" and click on bottom arrow to select a new interpreter

enter image description here

  • Click on "apply.

After these steps, you can change the interpreter any time.

1 Comment

thanks, but what you described is how to use two python versions for running apps, not for editing them. I.e., this does make pycharm parse files accordingly (python3 files will have 'errors' if you use python2 interpr for the project).

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.