Python Forum

Full Version: Getting a small Python2 prog to run in Python3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

I started to learn Python a few years ago, and wrote my only useful program in python2 - which I use all the time.

I want to start learning python again (py3), but when I install py3 my useful program won't run (it just flashes a window and closes).

I've forgotten everything I previously learnt, is there a 'fix quick' problem that jumps out as to why it wont run in py3 ?


It's a very simple program that monitors the clipboard for a block of text (they are links), and when it finds some it converts them into a block of links suitable for inserting into a webpage

Eg it finds on the clipboard

h+tp://anysite.com/file/textfile1
h+tp://anysite.com/file/textfile2


it converts that to
<a href=http://anysite.com/file/textfile1 target=_blank>http://anysite.com/file/textfile1</a>
<a href=http://anysite.com/file/textfile2 target=_blank>http://anysite.com/file/textfile2</a>

Then puts that new links back on the clipboard ready for pasting.


I have installed
Python2.7.13 (x86)
Pythoncard 0.8.2 (x86)
Pywin32-218
wxpython3.0 (x86)

I want to install Python3.

Thank you for any help,

Steve

import win32clipboard as w
import sys, time

def cleanboard():
    w.OpenClipboard(0)
    w.SetClipboardText('')
    w.CloseClipboard()

LastData=''
templine2=''
amountOfLines=0


cleanboard()


while 1:
    time.sleep(1)

    w.OpenClipboard()
    data=w.GetClipboardData()
    w.CloseClipboard()

    nullIndex=data.find('\0')
    if nullIndex != -1:
        data=data[:nullIndex]


    if data != LastData:
        LastData=data
        linkstoprint=[]
        templine2=''
        amountOfLines = len(data.splitlines());



        ref=0
        while ref < amountOfLines:
            line = data.splitlines()
            templine1=line[ref]
            templine1 = '<a href='+str(templine1)+' target=_blank>'+str(templine1)+'</a>'
            linkstoprint.append(templine1)
            ref=ref+1

        for i in range (0,amountOfLines):
            templine2=templine2+linkstoprint[i]+'\n'


        templine2=str(templine2)
        w.OpenClipboard(0)
        w.EmptyClipboard()
        w.SetClipboardText(templine2)
        w.CloseClipboard()
        LastData=str(templine2)
2to3 tool is useful to convert python2 code to python3.
In this case I don't think it will help, as I don't see any problems in your code. Do you have pywin32 installed for python3? You need to install it for every interpreter that will use it (i.e. if you installed it in python2, it doesnt work in python3)You may need to make some changes in the clipboard part of the code. You can also look at pyperclip.

Don't click on the program to start it, run it from command line so that you can see actual error.
Pywin32 is installed, I just re-installed to make sure - but it only gives me the option of installing it to Python2.
Running the program in an IDE gives

Error:
Traceback (most recent call last): File "F:\Windows Files\Documents\clipMonitor.py", line 1, in <module> import win32clipboard as w ModuleNotFoundError: No module named 'win32clipboard'
I don't remember installing that module separately ....... but damn it was a long time ago haha

EDIT: Ah, its part of pywin32.
I assume you have also python3 installed, right? From you answer I understand pywin32 is not installed for python3. You need to install it for python3 as well.
probably python and pip commands associated with python2
try with python3 and pip3

also, may look at pyenv for easy version management:https://python-forum.io/Thread-pyenv-Simple-Python-Version-Management
look also at https://python-forum.io/Thread-Python-3-...er-Windows
Yes, I have Python3 installed.

And yep, I just reached the same conclusion - no pywin32 installed for python3.

Brilliant - progress being made - Thank you :)