Preferred Method of Opening a PDF in Python 3 - python

Is there a newer way to open a PDF using an external viewer from Python 3 in Linux other than subprocess?
This sounds like a noobish and duplicate question, but I looked at this question and this question, and all of the answers are over 7 years old and recommended discouraged methods like os.system, old methods like manually creating a subprocess.Popen or Windows-only methods like os.startfile.
So in the time since these questions were answered, have preferred methods of launching a PDF reader from within Python emerged, or are these still the best answers?

Python as of 3.6 still doesn't have a cross-platform way to open files using default programs.
Issue 3177 suggested to add one, but it didn't happen yet.
So:
On Windows, there's a system call for this, you can reach it from Python via os.startfile,
On Linux, there's a command-line tool called xdg-open that does this,
On Mac OS, there's a command-line tool simply called open.
This means that unfortunately you still need to check the operating system and pick the right approach. The correct way to call the command-line tools is using the subprocess module.
This answer provides a code snippet:
Open document with default application in Python

Related

How can I write a Python script to control Gnome window placement?

I'm trying to wrangle my Gnome desktop windows and I would like to automate their placement using Python. What is a good approach for this?
You have 2 options:
If you only need it to work in an X session, then this is relatively easy to do using xdotool (short summary in this SO answer). You can also use it from Python, as explained in another question on SO.
The Wayland protocol does not allow this by default for security reasons. If you want to be able to support both X and Wayland, you'll have to get to the inner working of GNOME Shell by writing an extension (in JS). This extension then either does the moving, or exposes an API to move windows, which you can then call from a Python script.

Python Can I Print to Printer using Tkinter

I am making a notepad using Tkinter and I want to be able to print to the printer the notes taken.I am using a Text object in order to allow the user to write his notes.I couldn't find anything on to how to print to printer with Tkinter and I want to avoid using PyQT or win32api that I know have printer support(due to complexity and lack of experience).I am planning on releasing the application only on windows so I donnot need the it to be cross-platform.
Fact to Accept: Cross-Platform Printing is hard.
Depending on the system you use, the commands to send text / files to a printer will be very different.
For Windows, you should probably use the Python win32print module, even if you say you don't want to use it because of complexity etc.
I honestly think that trying to solve it any other way would be a lot more complicated in the end.
For Linux, Mac, Unix, you can send commands much more directly using LPR to the system printer through the built-in os.popen() or the new subprocess module in Python, but for Windows, my bet is you're better off using the win32print module.
Providing cross-platform printing functionality will always be a challenge because of the differences in the underlying sub-systems on different operating systems.
The basic approach
You'll need to separate the logic of your code, so that depending on the underlying OS, your program will choose the right method for executing the printing functionality you need.
I don't know of any way around this.
Using the Python win32 modules needn't be as complex as you might think.
For Windows
This can be done with the module win32print,
Documented nicely here
For Linux, macOS, Unix
Check out the use of LPR commands, and combine this with basic Python os.popen calls or using the new Python subprocess module
I know you probably wanted a more "copy/paste friendly" way, but that would be very hard without having the rest of your code and not knowing the exact requirements / specs for your app.
Bottom line is that you'd probably end up writing custom code for each platform for printing anyway, so might as well jump in head first.

How do I drag and drop onto a (python) script on OS X?

Is there any way to configure a recent OS X system so that python scripts automatically behave as drag and drop targets? If not, what is the most light-weight way (ideally scriptable) of configuring individual scripts for this behavior?
On Windows, I can simply drag and drop files onto a python script, and a python process will be launched with the full path names of the dropped arguments available in sys.argv. Amazingly, not only does this not happen on OS X, but there doesn't seem to be any way of configuring the system to do this automatically. All the solutions I've come across involve wrapping each of my scripts in an app.
Sure, this question has been asked and answered before. But not only is every single answer I could find limited to enabling drag and drop for one script at a time (by wrapping an app around them); it seems that all answers are obsolete and/or depend on broken links. Here's a sampler:
In python, how do I drag and drop 1 or more files onto my script as arguments with absolute path? (for windows, linux, and mac)
How do I execute a shell script with selected files/folders in Finder?
How to pass path names to Python script by “dropping” files/folders over script icon
As mentioned, none of them seem to work anymore. I have OS X 10.8, Mountain Lion, and I've seen zilch that could be expected to work on later OS versions. Any up to date solutions?
PS: Conceptually this question is not necessarily limited to python: I'm pretty sure that any mechanism that would work with shell or perl scripts would also work with python. But given how hard this seems to be on OS X, I'm keeping the question specific since that's my use case, and all solutions I've seen seem to be language-specific.
Have you seen this answer? Try using Mac OS X Automator (available in current and past versions of the OS--just tested this in Yosemite). As far as "one at a time" vs "multiple", this SuperUser post about the same subject explains how to make it work for multiple files.
At the end of the day, "Drag and Drop Support" is functionality of the Windowing System and has nothing to do with Python. That said, you can build OS X GUI apps in Python, which could be another solution to this. Check out this link about GUI Applications in Python for more info.

Execution permissions in Python

I need to send code to remote clients to be executed in them but security is a concern for me right now. I don't want unsafe code to be executed there so I would like to control what a program is doing. I mean for example, know if is making connections, where is connecting to, if is reading local files, etc. Is this possible with Python?
EDIT: I'm thinking in something similar to Android permission system. I want to know what a code will do and if it does something different, stop it.
You could use a different Python runtime:
if you run your script using Jython; you can exploit Java's permission system
with Pypy's sandboxed version you can choose what is allowed to run in your controller script
There used to be a module in Python called bastian, but that was deprecated as it wasn't that secure. There's also I believe something called RPython, but I don't know too much about that.
I would in this case use Pyro and write the code on the target server. That way you know clients can only execute written and tested code.
edit - it's probably worth noting that Pyro also supports http://en.wikipedia.org/wiki/Privilege_separation - although I've not had to use it for that.
I think you are looking for a sandboxed python. There used to be an effort to implement this, but it has been abolished a couple of years ago.
Sandboxed python in the python wiki offers a nice overview of possible options for your usecase.
The most rigourous (but probably the slowest) way is to run Python on a bare OS in an emulator.
Depending on the OS you use, there are several ways of running programs with restrictions, but without the overhead of an emulator:
FreeBSD has a nice integrated solution in the form of jails.
These grew out of the chroot system call.
Linux-VServer aims to do more or less the same on Linux.

What is the best method to call a Python 3.x program from within Python 2.x?

I'm writing a Django web application. As of now, Django does not support Python 3. For the purposes of my web application, and without getting into to much detail, I essentially need to use some libraries that only support Python 3. Suffice it to say that after much thorough research no 2.x alternative was found.
So my question is this: How should I go about this?
I have both Python 2 and 3 installed on my server, and I have the Python 3 code written and waiting to be called. I was considering simply using the subprocess module, effectively calling Python 3 from the command line, but the question is, is this the best method or is there a best practice I could use instead here? Using subprocess seems pretty hackish to me. Don't get me wrong, I'm okay with hackish, I just want to make sure there's nothing else I should be doing instead.
Since the Python 3 and Python 2 interpreters are totally separate executables and have separate libraries installed on your system, using subprocess to invoke one from the other is the best practice. It's not a hack at all. There are a number of ways to pass data between them but the two interpreters should be run as separate processes.
That said, you may need to keep in mind the startup time associated with launching an interpreter process. That gets back to how to pass data between the two processes. If your Python 2 code is going to be frequently calling the Python 3 routines, you may need to structure the Python 3 program as a daemon. But you would still use subprocess to launch it.
Run the 3.x program as a separate service and then connect using some kind of RPC mechanism?

Categories

Resources