Python Forum
Getting a small Python2 prog to run in Python3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting a small Python2 prog to run in Python3
#1
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)
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Yes, I have Python3 installed.

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

Brilliant - progress being made - Thank you :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad Migrating of python2 script to python3 zuri 7 873 Oct-05-2023, 02:40 PM
Last Post: snippsat
  Migration of Python2 and Python3 using Modernize and Future Rakshan 5 2,458 Oct-05-2023, 08:55 AM
Last Post: zuri
  python2 python3 messed up : How to fix ? hary 15 7,845 Dec-30-2020, 08:26 PM
Last Post: hary
  output mismatching when porting a python from python2 env to python3 env prayuktibid 2 2,517 Jan-21-2020, 04:41 AM
Last Post: prayuktibid
  python3 decoding problem but python2 OK mesbah 0 1,770 Nov-30-2019, 04:42 PM
Last Post: mesbah
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,823 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  python prog davy_yg 0 2,555 Sep-25-2019, 02:23 AM
Last Post: davy_yg
  Trying to run a python2 script dagamer1991 3 2,485 Aug-12-2019, 12:33 PM
Last Post: buran
  python2.7 executables thus the system python2.7 was erroring utility.execute()? vivekm 1 1,727 May-20-2019, 11:24 AM
Last Post: vivekm
  Python2 is not supported Skaperen 2 2,127 Mar-01-2019, 07:50 PM
Last Post: stranac

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020