Unable to initialize device PRN in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
3 min

banner

# Table of Contents

  1. Unable to initialize device PRN in Python
  2. Printing a file on Windows using the command line
  3. Make sure you have Python installed correctly

# Unable to initialize device PRN in Python [Solved]

The "Unable to initialize device PRN" error most commonly occurs when you issue the print file_name.py command.

To solve the error, use the python file_name.py command instead.

Here is an example of how the error occurs.

Suppose we have the following main.py file.

main.py
print('bobbyhadz.com') print('abc 123')

Running the following command in my terminal causes the error.

shell
print main.py

Make sure to replace main.py with the name of your Python script.

python unable to initialize device prn

The code for this article is available on GitHub

To solve the error, use the python main.py command instead.

shell
python main.py

issue python command

Make sure to replace main.py with the name of your Python script.

If you are on macOS or Linux, you might have to use the python3 command.

shell
# on macOS and Linux python3 main.py

If you are on Windows, you might have to use the py alias.

shell
# Windows (py alias) py main.py

issue py command

The code for this article is available on GitHub

If you tried to start the Python interpreter, issue the command without supplying a filename.

shell
python # or python3 python3 # or using the Windows py alias py

start python interpreter

If you need to exit the Python interpreter, call the exit() function.

shell
exit()

If you try to run Python commands after calling exit(), you will get the error because you've exited the Python interpreter.

You can resolve the issue by starting a new session of the Python interpreter by issuing the python command.

If the error persists and your filename contains the word print, try renaming the file.

# Printing a file on Windows using the command line

If you need to print a file on Windows using the command line, use the following command instead.

Note that you shouldn't pass a binary file to the prn port because the output won't be legible and you'll end up wasting paper.
shell
copy YOUR_FILE.txt lpt3 /b

Make sure to replace the YOUR_FILE.txt placeholder with the name of the actual file.

lpt3 is the printer port. If you get an error when using the lpt3 port, try using lpt.

shell
copy YOUR_FILE.txt lpt /b

You can also try using lpt1.

shell
copy YOUR_FILE.txt lpt1 /b

# Make sure you have Python installed correctly

If the error persists, make sure you have Python installed and added to your PATH environment variable.

You can download Python by visiting the official python.org website.

Once you download Python, make sure it has been added to your PATH environment variable.

You can verify that python has been installed and added to your PATH environment variable by issuing the python --version command.

shell
python --version

get python version

If you get an error when issuing the command, try to use python3 instead.

shell
python3 --version

If the issue persists and you are on Windows, try to use the py alias.

shell
py --version

If you get a version number after issuing the command, then everything works as expected.

If you get an error, then you have to download Python and add it to your PATH environment variable.

If none of the suggestions helped, try to restart your terminal session before issuing the command.

The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.