Working with a module name conflict

Sometimes module name conflicts occur between the application and the internal file in a third-party package. For example, a file named profile.py in the current folder will crash the jupyter notebook because it is trying to import it instead of its own profile.py . What is a good way to avoid this problem from the perspective of the package user? (Or is this something the package developer should somehow prevent?)

Note. While a similar problem occurs due to a collision between the application and the built-in names (e.g. time.py or socket.py ), it is at least relatively easy to remember the names of standard library modules and other built-in names in objects.

+5
source share
1 answer

The current directory is the directory that contains the main application script. If you want to avoid name collisions in this directory, do not put any modules in it.

Use a namespace instead. Create a package with a unique name in the main script directory and import all of this. The main script should be very simple and contain nothing more:

 if __name__ == '__main__': from mypackage import myapp myapp.run() 

All modules within the package must also use from import to access other modules within the package. For example, myapp.py may contain:

 from mypackage import profile 
+2
source

Source: https://habr.com/ru/post/1258692/


All Articles