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/05 02:12] – [Files: Reading From] gmancheat_sheets_python [2023/01/18 01:24] (current) – [Main Block] gman
Line 24: Line 24:
  
 ---- ----
 +===== Virtual Environment =====
 +
 +Create a new virtual environment for //**every**// Python project you work on ([[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 (avoiding conflicts).
 +  * 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 environments, install the ''python3-venv'' package. 
 +
 +**Create a Virtual Environment:** Make a new directory, cd into it, create the environment, and activate it.
 +
 +<code>
 +$ mkdir dir_name_whatever
 +$ cd dir_name_whatever
 +
 +$ python3 -m venv name_of_venv
 +#    -m : tells python to run the module as a script
 +#  venv : the module we want to run as a script
 +#  name : name it whatever you want
 +
 +$ source name_of_venv/bin/activate
 +#  runs the script to activate your virt env
 +#  your prompt should change: prefixed by name of venv
 +
 +$ deactivate  # exit the venv
 +</code>
 +
 +----
 +
 ===== Files: Reading From ===== ===== Files: Reading From =====
  
Line 46: Line 74:
  
 <code> <code>
-# A relative file path (starts in the directory in which you started Python+# A relative file path starts in the directory in which you started Python
 with open('files/text files/filename.txt') as file_object: with open('files/text files/filename.txt') as file_object:
  
Line 92: Line 120:
 ---- ----
  
-===== Check: File Load =====+===== Main Block =====
  
-Runs main() if file was not imported: +Runs ''main()'' if file was not imported: 
  
 <code> <code>
Line 101: 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.1672884720.txt.gz · Last modified: by gman