Writing a python script which asks to upgrade version - python-3.x

Is there any way to write a script and run it which asks the version of python being currently used, and if it's 2, install python3.
Also is there any way to install pip using script in the similar way.
New to python, sorry if there is any mistake.

In general, the answer is no. First of all, you would have to answer hard questions like whether it is acceptable to install a new runtime on the user's computer, and whether they would appreciate that.
One option is to simply bundle the Python interpreter you need in your installer. Never rely on the user's installed Python, unless perhaps they ask you to. Another option is to make your programs compatible with both Python 2 and Python 3--for some programs this is not a ton of work.

Related

How to choose and use a python3 dbus library to replace a dbus-send call

Using ubuntu 20.10
I want to write a python script to process the output of this shell command:
dbus-send --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.GetInhibitors
I don't know anything about coding dbus. The first place to start was I hoped a good python library.
I learn this about the apparently canonical library: "dbus-python is a legacy API, built with a deprecated dbus-glib library, and involving a lot of type-guessing (despite "explicit is better than implicit" and "resist the temptation to guess") (from https://wiki.python.org/moin/DbusExamples)
The library https://pypi.org/project/dbus-next/ promises a pure python implementation: "Zero dependencies and pure Python 3."
It is not mentioned on the wiki page mentioned above, but it looks like a healthy project.
However it seems that the pure python approach requires me to use the asyncio version. I think this is overkill for my needs.
I tried using dasbus but I can't install the necessary libraries, at least not in a virtual env.
Despite installing the system package python3-gi, I get errors "no module named gi" and trying to install PyGObject fails because "no package 'cairo' found" ... and ERROR: failed building wheel for pycairo.
So at this point, I have a library which is old and not recommended, a library with dependency difficulties and a library which seems to force me to use asyncio.
I now understand why the php script I am trying to re-write in python executed a shell command and dealt with the ugly output.
On top of that, I think I have worked out these points:
I need the SessionBus
The "path" is "/org/gnome/SessionManager"
The "interface" is org.gnome.SessionManager
I want to "call" the "member" GetInhibitors
I find async a bit of overkill since this is a shell script but if this is only way I can avoid a C dependency, I will deal with it.
Which library should I use?
pydbus is a very capable library that allows me to build things quickly and simply although the repo isn't necessarily that active.
I've only used dbus-next library a few times and it does have various *_sync methods if you don't want to do things asynchronously.

way to infer python version requirement?

When publishing a package, I need to tell users which version of python they require. I've been developing against Python 3.6, but I don't know how far backwards compatible my code is (or my dependencies).
Is there a static analysis tool / version guide to help me figure this out?
I'm sure this is impossible to do with regard to 3rd party dependencies, but is there a static analysis tool that might help? This seems like the sort of thing pylint could help with, but it doesn't appear to.
This answer seems to imply I might set up, for example, a conda environment for each sub version of python 3, install and run pylint from each. Maybe that's the best I can do?

Should I use Anaconda on Ubuntu WSL for other reasons than datascience?

I've been away from Python for a while (just a normal guy trying to learn it) and wanted to start learning again. I came across Anaconda and I am trying to figure out whether to use it or stick to pip. There are several questions for this, but they are all related to people using Python for Datascience, which I use R for.
So, is there any reason for me to use Anaconda if I am not planning on working with Datascience?
Sorry if this question seems easy to answer by searching, but I cannot find any information about this that isn't related to Datascience.
Off-hand, I'd say no. Anaconda simply clumps many (not all) of the standard data science-y packages with Python. If you go base Python, you get a much smaller install. Now one thing that is useful: conda. I find it's pretty good at managing package versions, which can be pretty problematic otherwise. But you can probably get by without it.

Python, kivy where to start?

New to programing(learning python3) and I'm wanting to use kivy. Just wondering how people recommend learning it all and what exactly needs to be learnt in order to make small games and apps(I'm thinking for android but not sure yet) using kivy and python. Basically what do I need to learn first, how much do I need to know and what(if anything) else needs to be learnt?
Check out this url
Where it reads:
Note
Python 3 support on Android is now available experimentally.
You may better practice on Python2 where Kivy works. I gave some hard shots on make it work on Python3 but it failed in most cases.

How to Be Python 3 Ready?

What are the current rules for writing python code that will pass cleanly through 2to3 and what are the practices that seem to be best suited to writing code that will not become mired forever in version 2.
I have read from the SciPy/NumPy forums that "100% test coverage" (unit testing) is important for many people, and I am not sure if that would apply to everybody. Certainly having a reasonable set of unit tests to try your code out with after conversion, seems a sane step.
Are there other things? What are skilled Pythonistas doing if they are writing 2.x code that they hope to have come through "cleanly" in the 2to3 process.
I am looking for specific instances of "[don't] do this" as well as some more general "best-practices", but specific instances of "do's and don'ts" are helpful.
Let's assume that frameworks, libraries (Django, SciPy/NumPy), and every other C Extension we need gets ported to Python3 eventually, and I'm asking about how you write and maintain the pure python language code that you write yourself.
Update: It's possible that what I really want is the "style guide" and list of deprecated features that everybody was already staying away from. I cut my teeth on Python 1.5 and moved to 2.0, and then have not really followed much of the 2.5/2.6 era, used them but really my code is more 2.1 era.
I'd say:
Read the "What's new for Python 3.0". Very informative.
In particular, if you care about Unicode or text encodings at all, take the time to understand what has changed for 3.x. That's probably one of the trickier things to change for Python 3.x.
Get Python 2.6 or 2.7, and run your code with the -3 flag. It will tell you about things in your code that will need changing.
Before using 3rd-party packages, check to see if they have a Python 3.x version. If not, check the package web site, mailing lists, version control repositories etc to see how actively the package is being developed and whether there is a roadmap towards Python 3.x support.
Download Python 3.x and try it out! Admittedly, that might not be practical if you care about code that currently depends on packages that don't yet support Python 3.x (e.g. wxPython or Django).

Resources