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
  •  | 
  •  

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

有帮助吗?

解决方案

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 ;-)

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top