The gMan nixWiki

Because the mind is made of Teflon...

User Tools

Site Tools


cheat_sheets_python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cheat_sheets_python [2023/01/18 00:56] – [Files: Reading From] gmancheat_sheets_python [2023/01/18 01:24] (current) – [Main Block] gman
Line 26: Line 26:
 ===== Virtual Environment ===== ===== Virtual Environment =====
  
-Create a new virtual environment for //**every**// Python project you work on.  +Create a new virtual environment for //**every**// Python project you work on ([[https://www.geeksforgeeks.org/python-virtual-environment/ | read]]).  
-  * Virtual environments allow you to keep all your project's dependencies separate from those of other projects you are working on (avoiding conflicts; [[https://www.geeksforgeeks.org/python-virtual-environment/ | read]]). This way the dependencies of each project are isolated from the system and from the other projects.+  * This way the dependencies of each project are isolated from the system and from the other projects (avoiding conflicts).
   * There are no limits to the number of environments you can have since they are just directories containing a few scripts.    * There are no limits to the number of environments you can have since they are just directories containing a few scripts. 
  
-**Installation:** To use virtual environmente, install the ''python3-venv'' package. +**Installation:** To use virtual environments, install the ''python3-venv'' package. 
  
 **Create a Virtual Environment:** Make a new directory, cd into it, create the environment, and activate it. **Create a Virtual Environment:** Make a new directory, cd into it, create the environment, and activate it.
Line 120: Line 120:
 ---- ----
  
-===== Check: File Load =====+===== Main Block =====
  
-Runs main() if file was not imported: +Runs ''main()'' if file was not imported: 
  
 <code> <code>
Line 129: Line 129:
 </code> </code>
  
----- +By including this ''if'' statement around your main program (i.e., not your imports, functions, classes, etc.), you can utilize your code in two ways:  
 +  When you run your program from the command line like normal, the program's name becomes ''__main__'' and therefore the main block is executed. 
 +  You can also import this program and its code into another program (as if it were a module or library) and have no side effects. 
 +    * Since ''__main__'' will refer to the other program into which you imported your code. Your imported program will act as a Python module and keep is own name (without the extension).  
 +    * This means everything else in your imported program (functions, classes, etc.) is available to you, but the main block will //**not**// be executed. Pretty slick
cheat_sheets_python.1674003386.txt.gz · Last modified: by gman