This article mainly introduces you to the method of handling modules and packages with the same name in Python. The introduction in the article is very detailed and has certain reference and learning value for everyone. Friends who need it can take a look below.
Preface
In programming development, I personally feel that as long as you follow the specifications, there will be few problems. . When you first start learning a technology, you will indeed encounter many pitfalls. It’s a good thing that you’ve stepped on more pitfalls. You’ll learn more, and you’ll feel more and more the importance of following the rules. The rules are formulated to avoid problems. Sometimes you really should listen to the advice of experienced people and don't go your own way. This does not seem to be the focus of this article. In fact, my focus is to express that we should try our best to do things according to the standards, so that we will avoid many detours.
The main programming language I use now is Python. After being exposed to Python so far, I feel that I have encountered very few pitfalls, and basically I have not encountered any strange problems. In fact, this is not a good thing. If you don’t step into the trap, you won’t understand many knowledge points lying in the dark, so it will be difficult to grow. Fortunately, there are some colleagues who know how to step into the trap.
A colleague asked me, in Python, if a module and a package have the same name, can only the package be imported? What should I do if I want to import the module? What he probably means is that in the same directory of the project, there is a foo.py file and a foo/ directory. If import foo is imported, the contents of foo/ will be imported instead of the contents of foo.py.
When I was asked this question, the first thing I felt was surprise. There was obviously ambiguity. If it were me, I would definitely not design the module name and package name to be the same, because essentially there is no way to distinguish who to import when importing. Unless the system has special provisions, for example, it is stipulated that only packages can be imported in this case.
I subconsciously think that an error should be reported here, because the Python interpreter does not know who to import. However, a colleague told me that other people's code is written like this, and in this case the package will be imported by default. That's possible, and the interpreter has stipulated that the package will always be imported in this case.
In order to verify this, I wrote a simple project with the following project structure:
. ├── main.py └── same ├── api │ └── init.py ├── auth │ └── init.py ├── auth.py └── init.py
Among them:
same/api/init/py content:
##
from .. import auth
auth_str = "This is str in package!"
auth_str = "This is str in module!"
from future import print_function from same.api import auth # Script starts from here if name == "main": print(auth.auth_str)
auth_str
string in the
same.auth package, and another in the same.auth module with the same name
auth_str string with the same name, then try to import auth in the same.api package, and finally try to output
same.api.auth.auth_str in main.py to see which string will is printed. At the same time, we tried to execute main.py with Python2 and Python3, and the results we got were:
This is str in package!
I am finally relieved now, and my previous conclusion has been confirmed. In Python, if you try to import a module and package with the same name, the package will be imported. In this case, if you want to import the module, you may need to use some 'hack' methods. There are some examples in the stackoverflow post mentioned above. Of course, the best way is to avoid such a design, so that you won't spend so long looking up information, and you won't spend so long writing articles similar to this article.
Summary
[Related recommendations]
1. Special recommendation:"php Programmer Toolbox" V0.1 version download
3. Python object-oriented video tutorial
The above is the detailed content of Solution to the duplicate names of Python modules and packages. For more information, please follow other related articles on the PHP Chinese website!