Init .py what is
The main. Without one, you cannot import modules from another folder into your project. The file essentially the constructor of your package or directory without it being called such. It sets up how packages or functions will be imported into your other files. Get matched to a bootcamp today. The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job. However, it is also used to set up imports, so they can be accessed elsewhere.
There are three main ways to do that:. Inside these files are functions that are unique to each file. In main.
The other imports the module — so we use dot notation to access the function names. Take notice that the all variable is surrounded by two underscores on either side.
Then we use dot notation to access the function name, as you can see above. Consider this example:. Our cakes folder would need to have these two files for our code to work:. The first file is our module. The second file tells Python that our directory contains Python modules. We could also import our code like this:. To learn more about importing modules, check out our Python import statement guide.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Ask Question. Asked 12 years, 10 months ago. Active 4 months ago.
Viewed 1. Mat Mat It's not the same thing as methane pointed out with an example here. Rainning A namespace package is not fundamentally different from a regular package. It is just a different way of creating packages.
Once a namespace package is created, there is no functional difference between it and a regular package. The official docs have a series of articles explain its usage, and they also defined every term precisely. Rainning maybe you didn't realize that explanation I added above comes from pep verbatim — Jun Show 1 more comment. Active Oldest Votes. Loki Loki What does this mean: "this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path"?
CarlG Python searches a list of directories to resolve names in, e. Because these can be any directory, and arbitrary ones can be added by the end user, the developers have to worry about directories that happen to share a name with a valid Python module, such as 'string' in the docs example. CarlG Try this. Make a directory called 'datetime' and in it make two blank files, the init.
Now open an interpreter, import sys, and issue sys. Now try something like from datetime import datetime;datetime. You should get an AttributeError because it is importing your blank file now. If you were to repeat these steps without creating the blank init file, this would not happen. That's what it's intended to prevent.
Add a comment. Given the example above, the contents of the init module can be accessed as import spam based on this. Shaido 24k 20 20 gold badges 61 61 silver badges 68 68 bronze badges. X and is still required under Python 2. See stackoverflow. An example Here is an example from one of my projects, in which I frequently import a sessionmaker called Session to interact with my database. The best way to learn is by doing! The code in this tutorial should work for Python 2 or 3.
Obviously, these functions are useless, but it helps to serve as a model for the basic concept that we have some python modules that we have already written that are somehow related. Well, we can only import these files if they are in the current directory that whatever script we are running is running from. To illustrate that, let's create a file called example1. So, now we can access our string functions in this manner. This is great, because they are all in a seperate folder, but the syntax is definitely not very succinct.
0コメント