Trying to wrap my head around changes in import python2 to python3
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Trying to wrap my head around changes in import python2 to python3
I have a small app written in python2 (works in 2.7) that I am trying to convert to python3. My version is 3.9.
The main part of the app is in one directory and there are additional modules nested one directory below. The sub-directory is named db. and contains modules Table.py, Column.py, etc.
So the relative path is db/Table.py etc.
The python2 syntax to import all the modules in the lower directory is "import db"
Python3 however gives me an error
Code:
import db
ModuleNotFoundError: No module named 'db'
I have searched through the changes and been unable to identify the correct way to restate the import for those modules.
Any suggestions on how to state that for successful import, or where to look for better info than I have located so far.?
The main part of the app is in one directory and there are additional modules nested one directory below
I made up a cheesy example for you. Since I did this test outside of python path, I appended the path.
Code:
~/test
|__init__.py
~/test/test2
|A.py
__init__.py
Code:
import sys
sys.path.append(sys.path[0] + '/..')
from test2 import A
class one():
def __init__(self):
#Local vars
self.Var1 = 'ABC'
self.Var2 = 'DEF'
print('class one, local Var1 =', self.Var1)
print('class one, local Var2 =', self.Var2)
#return a var from a function
def abc(self):
self.a = 5
self.Var3 = 'http://somewhere.com'
return self.Var3
abc(self)
if __name__ == "__main__":
obj1 = one()
obj2 = A.two(obj1)
A.py
Code:
class two():
def __init__(self, one):
#Local vars
self.Var1 = '123'
self.Var2 = '456'
print('class two, local Var1 =', self.Var1)
print('class two, local Var2 =', self.Var2)
#get vars from class one
self.Var4 = one.Var1
self.Var5 = one.Var2
print('class one Var1, shared with class two =', self.Var4)
print('class one Var2, shared with class two =', self.Var5)
#get var from function in class one
self.Var6 = one.Var3
print('class one funct Var3, shared with class two =', self.Var6)
Run __init__.py
Code:
python ~/test/__init__.py
class one, local Var1 = ABC
class one, local Var2 = DEF
class two, local Var1 = 123
class two, local Var2 = 456
class one Var1, shared with class two = ABC
class one Var2, shared with class two = DEF
class one funct Var3, shared with class two = http://somewhere.com
One of the 2→3 fixes futurize performs on your code is this:
Thank you for the hint and the link.
My biggest hurdle here is that I did not write the package and am trying to modify this one small part to work under python3.9
I ran futurize on the entire package and it ran cleanly.
However, now when I try to run it there is one import from python2 that seems to no longer exist. This error occurs even though my system still has python2.7 installed and functional.
Code:
import gtk
ModuleNotFoundError: No module named 'gtk'
reading further down into that part of the module I see several calls to gtk that I may need to modify for this to work once I have the correct replacement module imported.
I feel confident this error is because now the package calls python3 libs instead of the older python2 libs and I need to find the replacement that will work. I still have the functioning python2 version but am really trying to convert it to only python3.
Research shows that PyGObject should be the python3 replacement for PyGtk. On my system I have both PyGtk and PyGObject installed but cannot seem to figure out exactly what to import from PyGObject as a replacement for gtk for this use.
Any more pointers in this?
Last edited by computersavvy; 02-25-2021 at 04:37 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.