LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 02-23-2021, 09:34 PM   #1
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
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.?
 
Old 02-24-2021, 05:49 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
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
 
Old 02-24-2021, 07:46 AM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
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
 
Old 02-25-2021, 04:26 PM   #4
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Original Poster
Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by shruggy View Post
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.
 
Old 02-25-2021, 05:23 PM   #5
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Well, start with Getting Started
Code:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] standard output changes in python3 versus python2 sharky Programming 5 10-16-2018 04:30 PM
Can't wrap my head around it. permissions irridium77 Linux - Newbie 1 02-27-2015 07:00 AM
[SOLVED] Trying to install 'Gmail Backup' but it requires python2.5 - I have python2.8... Robert.Thompson Slackware 6 05-10-2011 08:23 AM
Trying to wrap my head around bash eveningsky339 Programming 5 12-02-2010 01:47 AM
Python2.5-devel is unable to find installed Python2.5 Setya SUSE / openSUSE 1 06-08-2007 01:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration