-1

I am trying to import variables defined in one python file in another file , both are in same directory. Below is the sample:

# filename1

class Proton:

 def test(self):

   self.a_tags = soup_file.find_all('a')
   self.cases = [str(each.get_text()) for each in self.a_tags]
   self.links = [(link.get('href')) for link in soup_file.find_all('a')]

# I want to import the above values in the below file

# filename2

import sqlite3

class Database:

    def __init__(self):

        self.create_connection()
        self.create_table()

    def create_connection(self):
        self.conn = sqlite3.connect('gopel.db')
        self.curr = self.conn.cursor()

    def create_table(self):

        self.curr.execute('''CREATE TABLE testfall(cases TEXT, links TEXT)''')

    def testfall_db(self):

        self.curr.execute('''INSERT INTO testfall VALUES(?,?)''',(self.cases,self.links,))
        self.curr.execute('''SELECT cases FROM testfall WHERE cases=?''', [self.cases])
        self.conn.commit()

I used from filename1 import * in filename2 but the values are still undefined and in the database its storing NULL values. Any suggestion on how to import these to the filename2 file.

PS: filename1 and filename2 are two different files

  • You only import `sqlite3`... Please provide a [mre] of the code and the error you get – Tomerikoo Jan 12 '21 at 20:32
  • 1
    The only variable defined in `filename1` is `Proton`. What other variables are you expecting to import? – Barmar Jan 12 '21 at 20:35
  • You can't import class attributes. In `testfall_db()`, `self` is a `Database`, not a `Proton`, so you can't access `self.cases` there. You need to create a `Proton`, then you can access its `cases` attribute. – Barmar Jan 12 '21 at 20:37
  • It seems that you are expecting `self.cases` which is a variable of an instance of `Proton` to be accessible within an instance of `Database`. – Countour-Integral Jan 12 '21 at 20:37
  • 2
    It seems like you have a fundamental misunderstanding about how objects work. – Barmar Jan 12 '21 at 20:38
  • You could do what you want if you make `Database` a subclass of `Proton`. – Barmar Jan 12 '21 at 20:38
  • This question and the answers may be helpful https://stackoverflow.com/questions/9455111/define-a-method-outside-of-class-definition – Stuart Jan 12 '21 at 20:49
  • @Barmar I have only specified a part of that file. I do have an understanding about how classes and Objects work. My question was related to only that part hence I have only put that part. I just want self.cases and self.links values to be used in filename2(different file). I need to fetch these values in the database that I have created in filename2. –  Jan 12 '21 at 22:23

2 Answers2

1

Great question!

The main problem here is that the variables that you're defining in the Proton class are "instance variables" (a.k.a. fields or data members) of a Proton object. Another way to think of this is that they "belong" to a certain Proton instance.

Without first creating that object, thought, they don't exist!

In the following example, we first initialize a Proton object, then access one of its instance variables.

my_proton = Proton() # Creating a Proton object

my_proton.a_tags # Accessing the a_tags attribute

Below, I have edited your code so that you can access the instance variables of the Proton object. In the example below, I just created tests tags to show how you can access them.

# filename1: "proton.py"  

class Proton:

  def __init__(self):
    self.a_tags = []
    self.cases = []
    self.links = []

  def test(self):
    self.a_tags = ['test a_tags']
    self.cases = ['test cases']
    self.links = ['test links']

# filename2: "database.py"

import sqlite3
from proton import Proton # from the file named "proton", import the Proton class

class Database:

  my_proton = Proton() # create a Proton object
  print(my_proton.a_tags) # access the a_tags object from the Proton object and print
JonShantz
  • 21
  • 3
0

Assuming you've imported Proton, if you want it to work with minimal changes:

def testfall_db(self):

    p = Proton() # need to create the instance first
    p.test()     # call the test method to initialize those values

    # access those values from the `p` instance of Proton created above
    self.curr.execute('''INSERT INTO testfall VALUES(?,?)''',(p.cases,p.links,))
    self.curr.execute('''SELECT cases FROM testfall WHERE cases=?''', [p.cases])
    self.conn.commit()

self.cases in your code for example, is calling the cases variable of the Database instance which does not exist -> None.