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