Is it possible to create a temporary python file and import the class and attribute of that .py file

StackOverflow https://stackoverflow.com/questions/22713876

  •  23-06-2023
  •  | 
  •  

Question

I have a requirement where, I have a python file on my desktop and want to create a temporary replica of it in my application and import its class objects. As the desktop ".py" file updates regularly.

Thanks in Advance guyz.. Please suggest me on this

Était-ce utile?

La solution

Yes, you can do it.

Only you need add the path of the files ".py" to sys.path variable, then you can import all modules locate in this path.

For example, suppose you have a couple of files in this path "/home/usuario/Desktop/my_proyect.py":

import sys
sys.path.append("/home/usuario/Desktop/")

import my_proyect 

I hope help you ;-)

Autres conseils

Yes, you can do it with importlib http://docs.python.org/2.7/library/importlib.html#importlib.import_module
Just pass the path to the temporary file to the import_module function.
I don't really understand why you need it but it's possible.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top