How .py Python import other files and their function


As shown above, I think I LstmParam and LstmNetwork import defined in lstm_1.py in test_1.py file. I direct the use of a reference method is the simplest: from lstm_1 import LstmParam, LstmNetwork, but obviously being given, this can not be quoted directly. Because the compiler environment can not be directly found.

When you import the, python interpreter only (a list, you can print out to see) in sys.path variable path inside the package to find a possible match or module.

The difference between a package with a common folder that, package folder more than a __init__.py file. In other words, if you add a __init__.py file in a folder, the python would think that this is a python folder in the package.

__Init__.py file content may be empty (package inside this module is necessary, .py file is the module, the knowledge to know), it just tells python current folder is a python in the package. Of course, you can add this __init__.py module inside some of the code that will run this package at import time, that is the next package __init__.py module, will be in the import package, immediately will never indented the place to start executing code.

So, make sure that the directory you want to import the py file comes from a __init__.py file.

When these problems arise that how can we succeed reference it? The solution is to say we need to refer to the target file into our site-pakage file compilation environment inside the package, then you can import up.



 

Guess you like

Origin www.cnblogs.com/zimo-jing/p/11274792.html