Skip to main content

Posts

Showing posts from September, 2019

Python Virtual Environments

I learnt about this early in the Python Course. I filed it away in my brain. Now that I'm learning about Django, it has come up again and all I can think is Virtual Environment who?? So for the benefit of my future junior software developer self, a virtual environment is a tool that enables one to keep  dependencies required by different projects together by creating an environment for them. In plain speak, I have a hypothetical box for Project A. It requires dependencies with specific versions- Dependency A ver 1.0, Dependency B ver 1.4, Dependency C ver 2.3.I put all these dependencies(with their specific versions) in that box. Now comes along Project B, which requires a different set of dependencies with different versions. Perhaps because the versions have since been updated. I store them all in hypothetical box 2. So each one of these hypothetical boxes is a virtual environment. It allows me to use different dependencies with my different projects because dependencies change

Importing infinity loop

I'm learning to set up a SQLite database in Python using the sqlite3 module. I've saved my .py file and imported sqlite3 and I'm ready to go. I try to create a database and AttributeError: module 'sqlite3' has no attribute 'connect' I can't, for the life of me, figure out what I'm doing wrong. Google is my friend. I named my file sqlite3.py in an attempt to make it easy to find this tutorial later on. BIG MISTAKE. Naming it sqlite3.py meant that my file was trying to import itself 😒😒😒. Obviously there is no attribute 'connect' in my own file. Note to self: Don't name files after software/languages/modules/etc.  Stupid mistakes Meh!

Snakes and ladders

I've started on my Python course. So far, the code has been familiar because the first few basic codes are similar to Javascript. And then modules happened. Confusion and despair! What is the world is 'if __name__ == "__main__": ' and why must I reach this section of my course on a public holiday when none of the instructors are in :( Stack overflow to the rescue, providing me a lifeline while I was drowning in a pit of serpents. I feel eternally indebted to a particular Mr Fooz.  Picture from  here From my understanding, when the Python interpreter reads a source file, it first sets the variable __name__ and then it executes all the code in the file. If that particular file that you are running(i.e. your module) is the main program, the interpreter will assign '__name__ = "__main__" '. Thereafter, any code in the aforementioned 'if' statement is run. If you have, instead, imported a module, the interpreter assigns '__name__