Python – Pass variables to imported modules

How to split your functions into modules and still keep the variables.

  1. Passing variables
  2. Summary

I recently had the problem that I could not pass my FTP object into a module that I had written myself.

The problem was that every time I had to reconnect to the FTP server and lost a lot of time.

Passing variables

Let’s say we have two Python script files. One of these is just the main script and the second file is

The is located in a subfolder called lib and is imported from there into my main script.

If we do that, the main script variables will not automatically be present in However, you can simply pass these by executing the following …

from lib import check
check.ftp_obj = ftp_obj
def myCoolFuntion(file):
  ftp_datetime = ftp_obj.voidcmd("MDTM " + file)[4:].strip()
  ftp_timestamp = time.mktime(time.strptime(ftp_datetime, '%Y%m%d%H%M%S')) 


With this method you can pass on as many variables as you like to another script. I used that with my ftp object and it worked as well. Have fun with it!


Experience Program python technology Tips

Beitrag teilen

WhatsApp it

Folgen Sie uns