Conditional compilation in PyCharm? (pypreprocessor?)



I am developing a Python/Kivy app which will have code that only run on a specific OS, like the following code, that only runs on Android:

from android import mActivity, autoclass, cast

File = autoclass('')
Intent = autoclass('android.content.Intent')

def create_file_intent(self, filepath, MIMEtype):
   context = mActivity.getApplicationContext()

(I hope to be able to code the same functionality for iOS etc. ...)

Ideally I need compiler directives understood by both PyCharm and the Kivy 'Buildozer' and Python compilers/interpreters.

In practice there exists a PyPI package called pypreprocessor - - which understands compiler directives for conditional compilation - for including and excluding sections of code based on 'constants' / 'flags' being true or false.
With that I could preprocess my python files before compiling them to Android or iOS with Buildozer. But how would that work together with PyCharm?
Or are there other ways?
What would be the most practical way of managing such a situation?


I have thought about it.

I guess the practical solution is to have the OS specific code in separate files, and comment out the import lines you don't need at the moment? Like this:

# import ios-specifics  # contains MyClass

# import android-specifics  # contains MyClass

import ubuntu-specifics  # contains MyClass



PS: I don't think it's very smart that all text here in the forum is light grey. That makes it harder to read. Almost as if the content is unimportant details that should rarely be read...

And why are the characters here in the comment editing box smaller than the already posted comments...?



PyCharm doesn't have any dedicated features for that. You could use conditional imports (i.e. check the OS your code is running on and use the corresponding import) if it's possible in your case, but PyCharm doesn't understand them yet so code completion and other features won't be different depending on the result of such evaluation.


Please sign in to leave a comment.