How to evaluate PEP-508-style platform markers?












2















I'd like to programmatically evaluate PEP-508-style platform markers such as platform_system == 'Windows'. It's not terribly challenging to implement a parser, but I assume there is one in pip. What is it called and how can I import & use it in my own script?










share|improve this question























  • pip does not have a public library API, and the pip devs don't want people importing & using the code in their own scripts. See the documentation for more information.

    – jwodder
    Nov 21 '18 at 17:58











  • Thanks for the heads-up. I kinda remembered that and since I'm controlling the whole environment, I'm fine with ignoring that.

    – Tamás Szelei
    Nov 21 '18 at 18:08
















2















I'd like to programmatically evaluate PEP-508-style platform markers such as platform_system == 'Windows'. It's not terribly challenging to implement a parser, but I assume there is one in pip. What is it called and how can I import & use it in my own script?










share|improve this question























  • pip does not have a public library API, and the pip devs don't want people importing & using the code in their own scripts. See the documentation for more information.

    – jwodder
    Nov 21 '18 at 17:58











  • Thanks for the heads-up. I kinda remembered that and since I'm controlling the whole environment, I'm fine with ignoring that.

    – Tamás Szelei
    Nov 21 '18 at 18:08














2












2








2








I'd like to programmatically evaluate PEP-508-style platform markers such as platform_system == 'Windows'. It's not terribly challenging to implement a parser, but I assume there is one in pip. What is it called and how can I import & use it in my own script?










share|improve this question














I'd like to programmatically evaluate PEP-508-style platform markers such as platform_system == 'Windows'. It's not terribly challenging to implement a parser, but I assume there is one in pip. What is it called and how can I import & use it in my own script?







python pip






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 17:54









Tamás SzeleiTamás Szelei

14.1k1379148




14.1k1379148













  • pip does not have a public library API, and the pip devs don't want people importing & using the code in their own scripts. See the documentation for more information.

    – jwodder
    Nov 21 '18 at 17:58











  • Thanks for the heads-up. I kinda remembered that and since I'm controlling the whole environment, I'm fine with ignoring that.

    – Tamás Szelei
    Nov 21 '18 at 18:08



















  • pip does not have a public library API, and the pip devs don't want people importing & using the code in their own scripts. See the documentation for more information.

    – jwodder
    Nov 21 '18 at 17:58











  • Thanks for the heads-up. I kinda remembered that and since I'm controlling the whole environment, I'm fine with ignoring that.

    – Tamás Szelei
    Nov 21 '18 at 18:08

















pip does not have a public library API, and the pip devs don't want people importing & using the code in their own scripts. See the documentation for more information.

– jwodder
Nov 21 '18 at 17:58





pip does not have a public library API, and the pip devs don't want people importing & using the code in their own scripts. See the documentation for more information.

– jwodder
Nov 21 '18 at 17:58













Thanks for the heads-up. I kinda remembered that and since I'm controlling the whole environment, I'm fine with ignoring that.

– Tamás Szelei
Nov 21 '18 at 18:08





Thanks for the heads-up. I kinda remembered that and since I'm controlling the whole environment, I'm fine with ignoring that.

– Tamás Szelei
Nov 21 '18 at 18:08












1 Answer
1






active

oldest

votes


















2














pkg_resources



pkg_resources (part of the setuptools package) provides marker evaluation function.



In [1]: from pkg_resources import evaluate_marker

In [2]: evaluate_marker('sys_platform == "darwin"')
Out[2]: True

In [3]: evaluate_marker('python_version > "3.7"')
Out[3]: False

In [4]: evaluate_marker('implementation_name == "cpython"')
Out[4]: True

In [5]: evaluate_marker('garbage')
Traceback (most recent call last):

File "/Users/hoefling/Library/Python/3.7/lib/python/site-packages/IPython/core/interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

File "<ipython-input-2-69434540d2ec>", line 1, in <module>
evaluate_marker('garbage')

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 1372, in evaluate_marker
raise SyntaxError(e)

File "<string>", line unknown
SyntaxError: Invalid marker: 'garbage', parse error at 'garbage'


etc.



packaging



Under the hood, pkg_resources.evaluate_marker uses packaging.markers.Marker.evaluate from the packaging package, so you can use that instead:



In [6]: from packaging.markers import Marker

In [7]: Marker('"linux" in sys_platform').evaluate()
Out[7]: False





share|improve this answer


























  • Perfect, thank you very much! This is exactly what I needed.

    – Tamás Szelei
    Nov 22 '18 at 10:31











  • Glad I could help!

    – hoefling
    Nov 22 '18 at 12:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417969%2fhow-to-evaluate-pep-508-style-platform-markers%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














pkg_resources



pkg_resources (part of the setuptools package) provides marker evaluation function.



In [1]: from pkg_resources import evaluate_marker

In [2]: evaluate_marker('sys_platform == "darwin"')
Out[2]: True

In [3]: evaluate_marker('python_version > "3.7"')
Out[3]: False

In [4]: evaluate_marker('implementation_name == "cpython"')
Out[4]: True

In [5]: evaluate_marker('garbage')
Traceback (most recent call last):

File "/Users/hoefling/Library/Python/3.7/lib/python/site-packages/IPython/core/interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

File "<ipython-input-2-69434540d2ec>", line 1, in <module>
evaluate_marker('garbage')

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 1372, in evaluate_marker
raise SyntaxError(e)

File "<string>", line unknown
SyntaxError: Invalid marker: 'garbage', parse error at 'garbage'


etc.



packaging



Under the hood, pkg_resources.evaluate_marker uses packaging.markers.Marker.evaluate from the packaging package, so you can use that instead:



In [6]: from packaging.markers import Marker

In [7]: Marker('"linux" in sys_platform').evaluate()
Out[7]: False





share|improve this answer


























  • Perfect, thank you very much! This is exactly what I needed.

    – Tamás Szelei
    Nov 22 '18 at 10:31











  • Glad I could help!

    – hoefling
    Nov 22 '18 at 12:27
















2














pkg_resources



pkg_resources (part of the setuptools package) provides marker evaluation function.



In [1]: from pkg_resources import evaluate_marker

In [2]: evaluate_marker('sys_platform == "darwin"')
Out[2]: True

In [3]: evaluate_marker('python_version > "3.7"')
Out[3]: False

In [4]: evaluate_marker('implementation_name == "cpython"')
Out[4]: True

In [5]: evaluate_marker('garbage')
Traceback (most recent call last):

File "/Users/hoefling/Library/Python/3.7/lib/python/site-packages/IPython/core/interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

File "<ipython-input-2-69434540d2ec>", line 1, in <module>
evaluate_marker('garbage')

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 1372, in evaluate_marker
raise SyntaxError(e)

File "<string>", line unknown
SyntaxError: Invalid marker: 'garbage', parse error at 'garbage'


etc.



packaging



Under the hood, pkg_resources.evaluate_marker uses packaging.markers.Marker.evaluate from the packaging package, so you can use that instead:



In [6]: from packaging.markers import Marker

In [7]: Marker('"linux" in sys_platform').evaluate()
Out[7]: False





share|improve this answer


























  • Perfect, thank you very much! This is exactly what I needed.

    – Tamás Szelei
    Nov 22 '18 at 10:31











  • Glad I could help!

    – hoefling
    Nov 22 '18 at 12:27














2












2








2







pkg_resources



pkg_resources (part of the setuptools package) provides marker evaluation function.



In [1]: from pkg_resources import evaluate_marker

In [2]: evaluate_marker('sys_platform == "darwin"')
Out[2]: True

In [3]: evaluate_marker('python_version > "3.7"')
Out[3]: False

In [4]: evaluate_marker('implementation_name == "cpython"')
Out[4]: True

In [5]: evaluate_marker('garbage')
Traceback (most recent call last):

File "/Users/hoefling/Library/Python/3.7/lib/python/site-packages/IPython/core/interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

File "<ipython-input-2-69434540d2ec>", line 1, in <module>
evaluate_marker('garbage')

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 1372, in evaluate_marker
raise SyntaxError(e)

File "<string>", line unknown
SyntaxError: Invalid marker: 'garbage', parse error at 'garbage'


etc.



packaging



Under the hood, pkg_resources.evaluate_marker uses packaging.markers.Marker.evaluate from the packaging package, so you can use that instead:



In [6]: from packaging.markers import Marker

In [7]: Marker('"linux" in sys_platform').evaluate()
Out[7]: False





share|improve this answer















pkg_resources



pkg_resources (part of the setuptools package) provides marker evaluation function.



In [1]: from pkg_resources import evaluate_marker

In [2]: evaluate_marker('sys_platform == "darwin"')
Out[2]: True

In [3]: evaluate_marker('python_version > "3.7"')
Out[3]: False

In [4]: evaluate_marker('implementation_name == "cpython"')
Out[4]: True

In [5]: evaluate_marker('garbage')
Traceback (most recent call last):

File "/Users/hoefling/Library/Python/3.7/lib/python/site-packages/IPython/core/interactiveshell.py", line 2961, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

File "<ipython-input-2-69434540d2ec>", line 1, in <module>
evaluate_marker('garbage')

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 1372, in evaluate_marker
raise SyntaxError(e)

File "<string>", line unknown
SyntaxError: Invalid marker: 'garbage', parse error at 'garbage'


etc.



packaging



Under the hood, pkg_resources.evaluate_marker uses packaging.markers.Marker.evaluate from the packaging package, so you can use that instead:



In [6]: from packaging.markers import Marker

In [7]: Marker('"linux" in sys_platform').evaluate()
Out[7]: False






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 2:46

























answered Nov 22 '18 at 2:39









hoeflinghoefling

12.6k43165




12.6k43165













  • Perfect, thank you very much! This is exactly what I needed.

    – Tamás Szelei
    Nov 22 '18 at 10:31











  • Glad I could help!

    – hoefling
    Nov 22 '18 at 12:27



















  • Perfect, thank you very much! This is exactly what I needed.

    – Tamás Szelei
    Nov 22 '18 at 10:31











  • Glad I could help!

    – hoefling
    Nov 22 '18 at 12:27

















Perfect, thank you very much! This is exactly what I needed.

– Tamás Szelei
Nov 22 '18 at 10:31





Perfect, thank you very much! This is exactly what I needed.

– Tamás Szelei
Nov 22 '18 at 10:31













Glad I could help!

– hoefling
Nov 22 '18 at 12:27





Glad I could help!

– hoefling
Nov 22 '18 at 12:27




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417969%2fhow-to-evaluate-pep-508-style-platform-markers%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?