Possible to have multiple SDKs/libraries in PyCharm?
已回答
This is a repost from the Slack intellij-platform channel per-request of Karol L.:
Is it possible to have a PyCharm project with its Python interpreter added as a dependency via something other than the one-and-only SDK for that project's one-and-only module? The same project in IntelliJ IDEA Ultimate Edition with its Python plugin works properly with the following:
<?xml version="1.0" encoding="UTF-8"?> <module type="WEB" version="4"> <component name="FacetManager"> <facet type="Python" name="Python"> <configuration sdkName="Python 3.11 (python)" /> </facet> </component> <component name="NewModuleRootManager"> <content url="file://$MODULE_DIR$"> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> </content> <orderEntry type="jdk" jdkName="NonPythonSdk" jdkType="NonPythonSdk" /> <orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="library" name="Python 3.11 (python) interpreter library" level="application" /> </component> </module>
but in PyCharm I get an error about an unsupported facet type and it seems to have to be configured as:
<?xml version="1.0" encoding="UTF-8"?> <module type="WEB" version="4"> <component name="FacetManager"> <facet type="Python" name="Python"> <configuration sdkName="Python 3.11 (python)" /> </facet> </component> <component name="NewModuleRootManager"> <content url="file://$MODULE_DIR$"> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> </content> <orderEntry type="jdk" jdkName="Python 3.11 (python)" jdkType="Python SDK" /> <orderEntry type="sourceFolder" forTests="false" /> </component> </module>
The issue is that I need this project to have the
It occurred to me that this question could potentially be asked in a different manner that's perhaps easier to answer. Can a single PyCharm project have multiple distinct virtual environments?
NonPythonSdk
as the SDK for its primary source files, but also be able to handle the project's Python files properly according to the venv
for the containing directory. Is that possible in PyCharm?It occurred to me that this question could potentially be asked in a different manner that's perhaps easier to answer. Can a single PyCharm project have multiple distinct virtual environments?
It's worth noting that I have technically found a workaround for this, but I would certainly prefer to avoid it if possible. The workaround requires me to change my plugin's current SDK type into a provided library added as a module dependency.
Not only does that have a significant ripple effect on my overall plugin which currently manages all of that as an SDK, but it means that for projects which don't have this Python "aspect", you end up with projects with no actual SDK. That seems to lead to a variety of other issues, e.g., the need for a custom
Not only does that have a significant ripple effect on my overall plugin which currently manages all of that as an SDK, but it means that for projects which don't have this Python "aspect", you end up with projects with no actual SDK. That seems to lead to a variety of other issues, e.g., the need for a custom
codeInspection.InspectionExtension
to prevent GlobalJavaInspectionContextImpl#isInspectionsEnabled
from saying that bulk inspection runs can't be executed for modules with no SDK.请先登录再写评论。
Quick update on this... I continued to figure out how to get this working the way I'd like/prefer in IntelliJ IDEA Ultimate Edition with one module for my plugin's contents and one module per-Python venv. I then opened that in PyCharm just to see what all would break...and...it works. Obviously modules aren't exposed in a first-class manner in PyCharm and the other lightweight IDEs, but evidently PyCharm is using them, e.g.:
and the following from the Python Integrated Tools config screen which clearly shows a module breakdown of the project even in PyCharm:
So...is it safe for me to rely on that? My own plugin is already going to be responsible for managing these modules and their internal configuration based on the corresponding Python venvs, so there won't be any need for end users to manage them explicitly.
I think this does exactly what I want/need, but I figure I'd ask if I'm coloring WAY outside the lines before committing to it. And if I am breaking the rules, what other options exist for supporting this type of thing in PyCharm in (as close to) the same manner that it works in IntelliJ IDEA Ultimate Edition?
Hi Scott! Yes, it's true that Inteliij Idea works with the code source on modules level and PyCharm works on a project level. Internally each PyCharm project is represented as a module in terms of intellij platform. And looks like you've already investigated it on your own. Yes, it's safe to rely on such model, because it exists from the very first days of PyCharm and we're not going to change it at the moment.
The other way to configure two independent environments in PyCharm is to open such folders as a separate projects, but in the same window. You can open folder `pythonfn` as a project, then go to File | Open, select folder `pythonfn2` and select "Attach" option in the dialog. After that both folders will be opened as a separate PyCham projects with separate environments, but in the same PyCharm window.
Did I get your question correctly? If not, feel free to elaborate on it. Thank you!
Hi, Elizabeth. Thank you for the information. I've tested my multi-module project approach extensively in PyCharm Professional and Community Editions and it seems to work very well, so I'm glad to hear that it's a sanctioned solution! As stated previously, my plugin fully manages the underlying modules and Python SDKs as its higher-level concepts are added/removed/updated, so it should be seamless for the end user. Unfortunately the multi-project approach won't work as well because it's important that the Python modules have access to the module for my plugin's custom language/SDK as well for certain interactions. Again, thank you for the follow-up and confirmation!