LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Trying to wrap my head around changes in import python2 to python3 (https://www.linuxquestions.org/questions/linux-software-2/trying-to-wrap-my-head-around-changes-in-import-python2-to-python3-4175691038/)

computersavvy 02-23-2021 09:34 PM

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.?

shruggy 02-24-2021 05:49 AM

One of the 2→3 fixes futurize performs on your code is this:
Quote:

Implicit relative imports fixed, e.g.:
Code:

- import mymodule
+ from __future__ import absolute_import
+ from . import mymodule



teckk 02-24-2021 07:46 AM

Quote:

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


computersavvy 02-25-2021 04:26 PM

Quote:

Originally Posted by shruggy (Post 6223969)
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?

shruggy 02-25-2021 05:23 PM

Well, start with Getting Started ;)
Code:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk



All times are GMT -5. The time now is 09:38 AM.