Daenyth
Daenyth

Reputation: 37461

Have intellij use different python SDKs for subdirectories in project

I have a python project like this:

project/
project/code
project/code/requirements.txt
project/fabfile
project/fabfile/requirements.txt

project/code is a Python3 project, so I need to use that SDK. project/fabfile has to be python2, because Fabric currently doesn't support python3.

On the command line I have two virtualenvs to manage this, but inside IntelliJ (With the python plugin), I have my SDK set to Python3 with my virtualenv for the project SDK. As a result, it isn't detecting the different requirements in fabfile/, and is marking python2 syntax as errors.

How can I get it to use different SDKs for these?

Upvotes: 0

Views: 614

Answers (3)

Lariel
Lariel

Reputation: 1

Assuming the following repository structure:

.
├── README.md
└── components
    └── module_A
        ├── src/...
    │   └── pyproject.toml
    └── module_B
        ├── src/...
        └── pyproject.toml

In IntelliJ IDEA 2024, with the JetBrains Python plugin:

Exclude each module from the root project (this is necessary to avoid conflicts in the next step):

  1. Go to File -> Project Structure -> Modules
  2. Choose your root module and click on the Sources tab
  3. Mark all modules as excluded (in my example I marked the entire components dir as excluded)

Add each module separately

  1. Go to File -> Project Structure -> Modules
  2. Click on + -> Import Module
  3. Select the module path inside your project (e.g.: components/module_A)
  4. Select Create module from existing sources and click Next
  5. Select only the first checkbox (should match the module path that you chose on step 3), then click on Next and Create
  6. The new module will appear in the list. Right-click on it, then click on Add -> Framework -> Python. This will open a window for you to configure the Python interpreter for that module.

I haven't tested running code inside the IDE after this, but I'm able to use full code completion and navigation in the files under each module, with all and only the dependencies of that module's interpreter being visible.

Upvotes: 0

yole
yole

Reputation: 97288

If you're using IntelliJ IDEA, you can set up a multi-module project, and assign a different interpreter to each module. Create one module with 'code' as the content root, and another with 'fabfile' as the content root.

Upvotes: 1

FintanH
FintanH

Reputation: 110

As far as I know, I don't think can you use different interpreters simultaneously

You would have to switch interpreters on each project

Go to Preferences > Project Interpreter and point the interpreter to your virtual env(s)

Upvotes: 0

Related Questions