0

I have two Python (3.8) scripts located in the same folder.

The first lookup.py is simply:

#! /usr/bin/env python3
import os
from getimp import session0
print (session0)

The second script getimp.py identifies a cookie and sets it as a variable which is imported into the first script. I have omitted some of the code here, but hopefully have the critical parts.

#! /usr/bin/env python3

import os
import json
import base64
import sqlite3
import shutil
from datetime import datetime, timedelta
import win32crypt # pip install pypiwin32
from Crypto.Cipher import AES # pip install pycryptodome

def get_chrome_datetime(chromedate):
    """Return a `datetime.datetime` object from a chrome format datetime
....

....
    # you can also search by domain, e.g thepythoncode.com
    cursor.execute("""
    SELECT host_key, name, value, creation_utc, last_access_utc, expires_utc, encrypted_value
    FROM cookies
    WHERE name like '%user_id%'""")
    # get the AES key
    key = get_encryption_key()
    for host_key, name, value, creation_utc, last_access_utc, expires_utc, encrypted_value in cursor.fetchall():
        if not value:
            decrypted_value = decrypt_data(encrypted_value, key)
        else:
            # already decrypted
            decrypted_value = value
        print(f"""
        {decrypted_value}
        ===============================================================""")
        
    session0 = decrypted_value

if __name__ == "__main__":
    main()

If I run getimp.py on its own it generates the correct result but when I run lookup.py I get an error:

  File "lookup", line 4, in <module>
    from getimp import session0
ImportError: cannot import name 'session0' from 'getimp' (D:\Documents\ptest\getimp.py)

Am I losing the variable once the script getimp.py finishes?

3
  • I don't see a top-level session0 variable in getimp.py. There's a session0 variable inside the function get_chrome_datetime(), but that variable shouldn't be visible outside that function. Commented Nov 30, 2021 at 17:40
  • your session0 variable seems to be local to the get_chrome_datetime function, try making it global Commented Nov 30, 2021 at 17:42
  • (I'm new to this!) So how do I get that value from inside the function to the top level so that presumably it can then be passed on? Thanks. Commented Nov 30, 2021 at 17:43

2 Answers 2

1

Your problem is that the variablesession0 is defined inside the scope of get_chrome_datetime and therefore can not be addressed from import.

Try importing the function and create the variable inside the scope of the active script.

Inside 'get_chrome_datetime' change session0=decrypted_value into return decrypted_value

and in lookup.py :

import os
from getimp import get_chrome_datetime
print (get_chrome_datetime(argument))
Sign up to request clarification or add additional context in comments.

2 Comments

I can't quite get this yet. How do I define the variable 'argument'?
in getimp.py you give define 'get_chrome_datetime' to take an argument that you call 'chromedate', just give what 'chromedate' should be instead of argument
1

Your problem is that session0 is defined inside the function.

I would suggest the following

session0 = None
def get_chrome_datetime(chromedate):
  global session0
  ... (your code here)

Also you should call the function outside of if __name__ == '__main__' because when you're importing a module, the __name__ wouldn't be "__main__"

4 Comments

I understand the first part, but what do I need to do to if name == "main": main() ?
name == "main" is used when you are running the script, not importing it. It's used to prevent your code from running when you're importing it from an other file. This one really depends on what you're trying to do, since you can return the session0 value & simply import that function instead of importing the variable
No, I would not run this script on its own, only imported into lookup.py. So sounds like I can delete those lines. They are only there because I adapted the code from thepythoncode.com
Also, main() is going to run the get_chrome_datetime function ? If so, you can remove the if name == .... Or you can make your function return the value session0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.