RDS
Posts: 773
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

How do I print a Python Program

Thu Jul 09, 2020 6:36 pm

I am trying unsuccessfully to print out the code of a Python3 program.

I have followed the set up instructions for my Printer and I can print out the test page. I can also type some text into Libre Office and print that without problems. However, when I try to print a Python Program (which I think just makes it easier to follow and debug) the pi returns the following error:
Printing command: "1pr'/home/pi/Desktop/prog1.py' 2>&1"Printing failed (exit status 0x7f00)/bin/sh: 1pr: not found

Could someone please advise if it is actually possible to print the program code out and if so how I can overcome the above error.
Thank you

klricks
Posts: 7135
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: How do I print a Python Program

Thu Jul 09, 2020 6:55 pm

I don't know how to fix your issue but a workaround might be easier...
- Copy & Paste the scrirpt into LibreOffice doc.
- Save to a USB stick and move to a different computer
- Email the file to yourself with a web based email then retrieve on a different computer.
etc
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

RDS
Posts: 773
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: How do I print a Python Program

Thu Jul 09, 2020 7:09 pm

Thanks,
I have previously copied the text and printed using something like MS Word on a Windows PC but it does not retain the colour aspects. I have not tried using Libre Office but if that does retain the colour and indent s that would be a good solution, as I can actually print from Libre Office.

I was just hoping to do it directly from within the IDE.

User avatar
Paeryn
Posts: 2952
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: How do I print a Python Program

Thu Jul 09, 2020 7:35 pm

RDS wrote:
Thu Jul 09, 2020 6:36 pm
I am trying unsuccessfully to print out the code of a Python3 program.

I have followed the set up instructions for my Printer and I can print out the test page. I can also type some text into Libre Office and print that without problems. However, when I try to print a Python Program (which I think just makes it easier to follow and debug) the pi returns the following error:
Printing command: "1pr'/home/pi/Desktop/prog1.py' 2>&1"Printing failed (exit status 0x7f00)/bin/sh: 1pr: not found

Could someone please advise if it is actually possible to print the program code out and if so how I can overcome the above error.
Thank you
You've got the name of the command wrong, the first character is a lower-case L not the digit 1 as you wrote it. It's lpr, short for line print.

Code: Select all

lpr /home/pi/Desktop/prog1.py
She who travels light — forgot something.

pcmanbob
Posts: 9298
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How do I print a Python Program

Thu Jul 09, 2020 7:37 pm

As I do most of my programming on my desktop pc running windows as my pi's tend to run headless, I use winscp to connect to my pi and on my desktop PC I use notepad++ to edit/write code.

So if I want to print some code I just select the file in question in winscp directly on the pi then edit it selecting external editor ( notepad++ ) and print the code from notepad++ it prints the code exactly as displayed on the screen with correct indentation and colour highlighting.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How do I print a Python Program

Thu Jul 09, 2020 7:50 pm

RDS wrote:
Thu Jul 09, 2020 6:36 pm
Printing command: "1pr'/home/pi/Desktop/prog1.py' 2>&1"Printing failed (exit status 0x7f00)/bin/sh: 1pr: not found
It's (ell-pee-arrh)lpr /home/pi/Desktop/prog1.py 2>&1 not (wun-pee-arrh) 1pr'/home/pi/Desktop/prog1.py' 2>&1

You also need to run sudo apt install cups-bsd to get the lpr command installed (along side the other cups pieces for line command printing).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

RDS
Posts: 773
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: How do I print a Python Program

Thu Jul 09, 2020 9:18 pm

@Paeryn & DougieLawson
Yes, sorry I copied the error incorrectly. Now that you point it out, it is as you have both stated. However, that was an error that the pi displayed when I tried to use the Print option in the IDLE File drop down.

@DougieLawson
I was not trying to print from the command line but I don't recall having added that sudo command when I tried to install the printer a couple of weeks ago. I will give that a go.
Thank you
Last edited by RDS on Thu Jul 09, 2020 9:35 pm, edited 1 time in total.

RDS
Posts: 773
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: How do I print a Python Program

Thu Jul 09, 2020 9:27 pm

@DougieLawson
Thank you. That sudo command has worked and now it will print using the Print option in the File Tab. It only printed in Monochrome though and completely ignored the colour which is present in the IDLE program listing. I will try pasting the Program into Libre Office to see if that retains the colour.

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How do I print a Python Program

Thu Jul 09, 2020 9:29 pm

That sudo command was just to get the cups compatible lpr program installed. It's a serious illness if you catch a dose of sudoitis, the prognosis is that you will ultimately result in destroying a filesystem when you least expect it (by sudo misuse).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

RDS
Posts: 773
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: How do I print a Python Program

Thu Jul 09, 2020 9:38 pm

DougieLawson wrote:
Thu Jul 09, 2020 9:29 pm
That sudo command was just to get the cups compatible lpr program installed.
Thanks. I only use it sparingly and only then if advised by someone I trust!

RDS
Posts: 773
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: How do I print a Python Program

Thu Jul 09, 2020 9:43 pm

@pcmanbob
I also use all my pi's headless, in my case using VNC. Could you explain more about winscp please as I have now tried to paste the program listing into LIbreOffice and although it copies across, all the colour highlighting is lost and it the colour that makes the program far more readable.

Its not as if the pi cannot send colour because the Test Page that it did print out, has a colour logo on it.

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How do I print a Python Program

Thu Jul 09, 2020 10:06 pm

RDS wrote:
Thu Jul 09, 2020 9:38 pm
DougieLawson wrote:
Thu Jul 09, 2020 9:29 pm
That sudo command was just to get the cups compatible lpr program installed.
Thanks. I only use it sparingly and only then if advised by someone I trust!
So not me then. With my efforts today (I just could not get things working on my mainframe) I don't trust me. :roll:
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
Paeryn
Posts: 2952
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: How do I print a Python Program

Thu Jul 09, 2020 10:56 pm

RDS wrote:
Thu Jul 09, 2020 9:43 pm
Its not as if the pi cannot send colour because the Test Page that it did print out, has a colour logo on it.
IDLE doesn't support printing in colour natively, it just sends the raw text file to the printer.

There is an extension for it called idle2html that will "print" to an HTML file complete with syntax highlighting which you could then open in your browser of choice and print from there. I just tried installing it using pip3 but it failed (it downloaded but couldn't run the setup). Might be that it wasn't updated for Python3 (but I would've thought pip3 wouldn't list it if there wasn't a Python3 version).
She who travels light — forgot something.

klricks
Posts: 7135
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: How do I print a Python Program

Fri Jul 10, 2020 12:53 am

RDS wrote:
Thu Jul 09, 2020 9:27 pm
@DougieLawson
Thank you. That sudo command has worked and now it will print using the Print option in the File Tab. It only printed in Monochrome though and completely ignored the colour which is present in the IDLE program listing. I will try pasting the Program into Libre Office to see if that retains the colour.
I am not sure but It may be that the colors are not actually part of the script file and are dynamically added by the IDE after loading the file each time.
I know that I get a different color scheme when I load my file into Thonny vs nano.
You might try to open your file into a Windows IDE.........
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

W. H. Heydt
Posts: 12431
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: How do I print a Python Program

Fri Jul 10, 2020 1:04 am

RDS wrote:
Thu Jul 09, 2020 9:18 pm
@Paeryn & DougieLawson
Yes, sorry I copied the error incorrectly. Now that you point it out, it is as you have both stated. However, that was an error that the pi displayed when I tried to use the Print option in the IDLE File drop down.

@DougieLawson
I was not trying to print from the command line but I don't recall having added that sudo command when I tried to install the printer a couple of weeks ago. I will give that a go.
Thank you
As people have shown in their replies, you also need a space between lpr and the name of the file to be printed. Another command you can use is "lp". Here is a bit of C code issuing a command to print a file to a specific printer (it will select which printer depending on what is in the file, but that is a different discussion):

Code: Select all

 sprintf(cmd,"lp -oraw -d %s %s",
                  printer_id, ptr_file_name);
system(cmd);
The sprintf builds the string that is the command to be printed. The "system(cmd)" actually tells Linux to execute the command. The "-oraw" flag tells the printer that it is getting a "raw" file and just to do what the file tells it to. There are PCL5 commands embedded in the files in question.

pcmanbob
Posts: 9298
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How do I print a Python Program

Fri Jul 10, 2020 8:23 am

RDS wrote:
Thu Jul 09, 2020 9:43 pm
@pcmanbob
I also use all my pi's headless, in my case using VNC. Could you explain more about winscp please as I have now tried to paste the program listing into LIbreOffice and although it copies across, all the colour highlighting is lost and it the colour that makes the program far more readable.

Its not as if the pi cannot send colour because the Test Page that it did print out, has a colour logo on it.
WINscp : https://winscp.net/eng/index.php
WinSCP is a popular SFTP client and FTP client for Microsoft Windows! Copy file between a local computer and remote servers using FTP, FTPS, SCP, SFTP, WebDAV or S3 file transfer protocols.
On the pi it uses the same assess a SSH and allows you to copy files from your pi to your PC or your PC to your pi, by simply dragging and dropping.

So once SSH is enabled on your pi and WINscp is installed on you PC and assuming both are on the same network you simple enter the IP address of your pi in to the required filed along with your user ID ( pi ) and the relevant password and click logon.

Image

once connected you will be presented with this screen

Image

on the left are the files on your PC you can navigate to any directory you wish on the right are files on your pi again you can navigate to any directory your wish.

notepadd++ : https://notepad-plus-plus.org/
now if you have notepad++ installed on your PC you can open a python file on your pi directly in notepad++ on your PC by simply right clicking on the file in question and selecting edit then selecting notepad++

Image

excuse the poor quality could not use my screen capture and retain the selections so had to use my phone to take picture.

once open in notepad++ you can simple print directly form there retaining indentation and colour highlighting.

Image

hope this helps..
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

nliviu
Posts: 13
Joined: Tue Jun 23, 2020 1:24 pm
Location: Romania

Re: How do I print a Python Program

Fri Jul 10, 2020 9:04 am

There is a Notepad++ plugin (NppFTP) which allows to sftp in a remote server.
nppftp.JPG
nppftp.JPG (81.98 KiB) Viewed 256 times

RDS
Posts: 773
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: How do I print a Python Program

Fri Jul 10, 2020 1:21 pm

@pcmanbob
Thank you very much for that. I am out most of the day but I will try that later.

spectrum6
Posts: 30
Joined: Sun Jun 14, 2020 12:40 pm

Re: How do I print a Python Program

Fri Jul 10, 2020 4:45 pm

If your using vim on buster, and you have lpr working with a default printer then you can just type ":hardcopy" in vim (not in editing mode). It will send the document with syntax highlighting & color to the printer.

If you want to write a PostScript file to disk you can do ":hardcopy >/tmp/test.ps".

Make sure you have vim installed with the syntax highlighting working, and not the default alternative (vim tiny does not have :hardcopy). I haven't tested it on the vim installed with buster (8.1), but I have tested it with vim (8.0) on Ubuntu 18.04.

RDS
Posts: 773
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: How do I print a Python Program

Fri Jul 10, 2020 9:52 pm

@pcmanbob
Thank you for those very detailed instructions.
I have carried out all the steps and all the files of the pi are visible on my PC and I can copy them across.
However, when I right click on the file, I get a similar screen to that you showed and an option to edit in Notepad but not Notepad++ as that is not listed.

Notepad++ is loaded on my PC and I can click the Destop icon it created and a Notepad++ window opens.

pcmanbob
Posts: 9298
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How do I print a Python Program

Sat Jul 11, 2020 7:27 am

RDS wrote:
Fri Jul 10, 2020 9:52 pm
@pcmanbob
Thank you for those very detailed instructions.
I have carried out all the steps and all the files of the pi are visible on my PC and I can copy them across.
However, when I right click on the file, I get a similar screen to that you showed and an option to edit in Notepad but not Notepad++ as that is not listed.

Notepad++ is loaded on my PC and I can click the Destop icon it created and a Notepad++ window opens.
It was a long time ago when I set this up may be I added notepad++ to winscp manually .

open winscp connection to a pi, then select options preferences

Image

then select editors ( you can see I already have an entry for notepad++ )

Image

then click on add, select external editor then click on the browse button and locate the file notepad++ once you select this the add editor window should look like this.

Image

now just click OK and OK

now you should have notepad++ in your options when selecting edit.

Image
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

RDS
Posts: 773
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: How do I print a Python Program

Sat Jul 11, 2020 1:02 pm

@pcmanbob
Thank you very much. I really appreciate the time you have spent helping me.
I needed to load Notepad++ manually and the setup followed your pictures perfectly.
I am now able to print my Python programs and see the colour highlights

I wonder if I have a different version of WinSCP to you (mine is v7.8.8 63 bit) because the tabs across the top are different to yours.
I have File-Commands-Mark-Session-View-Help
You have Local-Mark-Files-Commands-Session-Options-Remote-Help
I was able to access the Properties from an icon near the top of the screen to load Notepad++ as an editor.

This may just be different preferences we have set up.
Anyway, it is all working fine now, thanks again.

GilGill
Posts: 2
Joined: Mon Jul 06, 2020 12:50 pm

Re: How do I print a Python Program

Sat Jul 11, 2020 1:06 pm

On my RasPi's, I always install geany, which I use as my generic code editor for 'C', Java, Python, shell scripts, etc.
It has pretty printing capability.

pcmanbob
Posts: 9298
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How do I print a Python Program

Sat Jul 11, 2020 1:15 pm

Glad you have it working , my WINscp is currently 5.17.6, I will look see if it needs an update, but it normally warns me there is an update.

looking at the site I linked https://winscp.net/eng/download.php I do have the latest copy for windows

Image

So I am not sure what you down loaded especially if its v7.8.8 63 bit :shock: but it may be depend upon which version of windows you are running ( examples from windows 7 pro 64bit)

Edit ....

It looks like you might be using The Explorer Interface which is similar to Windows File Explorer,
where as I am using the Commander Interface which is based on Norton Commander and similar file managers.

should you want to try the other interface

Image
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

RDS
Posts: 773
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: How do I print a Python Program

Sat Jul 11, 2020 2:37 pm

Yes, I am using the Windows Explorer version with 64 bit W10 Pro.
I saw the word Norton and went for the alternative.

I am hoping that WinSCP will also enable me to copy programs easily from one pi to another but that is for another day.

Return to “Beginners”