简体   繁体   中英

Can I create a data file in Python and import it to other python files

I have created a python file that consists some data. I m able to import this data file to a different python file, but my concern is that I can import only one variable at a time and not all the variables at once.

from sourcefilename import RequiredVariable

I want to import and use all the variables in one call. Is that possible.

from sourcefilename import *

This is really not recommended, though. It lets you imports (essentially, execute) any arbitrary code from the module sourcefilename , and then simply copy all global names from that module into your module (overwriting anything previously defined in your module). You should use it only if you can be very certain of the exact data structure that goes into sourcefilename . However, if that is the case, you may as well use a more conventional method for storing data, like JSON or pickle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM