A bit confused with python import

I come from PHP (as well as many other things) and I play with Python. In PHP, when I want to include another file, I just do include or require and everything in this file is included.

But it seems that the recommended way to do things in python is from file import , but does it seem to be more for inclusion in libraries and stuff? How do you separate your code from multiple files? Is this the only way to do this, to have one file with a whole set of function calls, and then import 15 other files?

+2
python import module
Jun 22 '13 at 19:39
source share
5 answers

All things are completely different between PHP and Python, and there are many reasons.

But it seems that the recommended way to do things in python is from file import , but it seems like more to include libraries and other things?

Indeed, import statements are designed to import objects from another module into the current module. You can import all objects of the imported module into the current module:

 import foo print foo.bar 

or you can choose what you want from this module:

 from foo import bar print bar 

and even better, if you import a module twice, it will be imported only once:

 >> import foo as foo1 >> import foo as foo2 >> foo1 is foo2 True 

How do you separate your code from multiple files?

You need to think about your code ... This is called software design, and here are a few rules:

  • you never write an algorithm at the module level; instead make it a function and call that function
  • you never instantiate an object at the module level; you must embed it in a function and call that function
  • If you need an object in several different functions, create a class and encapsulate this object in this class, and then use it in your functions associated with this class (so now they are called methods)

The only exception is when you want to run the program from the command line by adding:

 if __name__ == "__main__": 

at the end of the module. And my best advice would be to just call your first function right away:

 if __name__ == "__main__": main() 

Is this the only way to do this, to have one file with a whole set of function calls, and then import 15 other files?

This is not the only way to do this, but it is the best way to do it. You do all your algorithms in function and object libraries, and then import exactly what you need in other libraries, etc. This is how you create an entire reusable code universe and you should never reinvent the wheel! Therefore, forget about files and think about modules containing objects.

Finally, my best tip for you learning python is to unlearn every habit and use that you used when coding PHP, and learning all this differently. In the end, it can make you a better software engineer.

+7
Jun 22 '13 at 21:48
source share

I suppose I understand what you want to say and do.

Here is an example of random inclusion from PHP:

File # 1 - vars.php

 <?php $color = 'green'; $fruit = 'apple'; ?> 

File # 2 - main.php

 <?php echo "A $color $fruit"; // A include 'vars.php'; echo "A $color $fruit"; // A green apple ?> 

The fist echo command will print only the string "A", since it does not have the values ​​assigned to vars. The next echo will print the complete line, thanks to your inclusion in front of it.

The Python import, however, imports a module or part of it, so you can work with it in your current module.

Here is a python example:

File 1 - echo.py

  apple = 'apple' color = 'green' 

File 2 - main.py

 import echo def func(): print "A "+echo.color+" "+echo.fruit if __name__ == '__main__': func() 

In other words, you import some functions from one module, and then use it in your other module.

The above example is not very good at programming standards or best practices, but I think this gives you some insight.

+2
Jun 22 '13 at 21:33
source share

Interest Ask. As you know, in PHP you can separate your code with include, which literally takes all the code in the included file and puts it where you called include. This is convenient for writing web applications because you can easily split a page into parts (e.g. heading, navigation, footer, etc.).

Python, on the other hand, is used for more convenience than just web applications. To reuse code, you must rely on functions or the old object-oriented programming. PHP also has functions and object-oriented programming FYI.

You write functions and classes to a file and import them into another file. This allows you to access functions or use the classes you define in another file.

Suppose you have a function called foo in file1.py . From file2.py you can write import file1 . Then call foo with file1.foo() . Alternatively write from file1 import foo , and then you can call foo with foo() . Note that from allows you to call foo directly. See python docs for more info.

+1
Jun 22 '13 at 19:51
source share

At a technical level, Python imports are very similar to PHP, as it will execute the imported file. But since Python is not designed to create an HTML file, the way you use it is very different.

Typically, a module-level Python file does not include much executable code at all, but rather definitions of functions and classes. You import them and use them as a library.

Therefore, having things like header () and footer () doesn't make sense in Python. These are just functions. Call them that and the result they generate will be ignored.

So how did you split your code in Python? Well, you divided it into functions and classes that you insert into different files and then import.

+1
Jun 22
source share

There's an execfile() function execfile() that does something vaguely comparable to PHP include , but it's almost certainly what you don't want to do. As others have said, this is just a different model and a different programming need in Python. Your code will go from function to function, and it really doesn't matter what order you put them in, if they are in the order in which you define things before using them. You are simply not trying to find some sort of ordered document similar to PHP, so there is no need.

0
Jun 23 '13 at 2:24
source share



All Articles