Tales of a Techie learning Python
Tags: python, dailylearning, programming
Published: 2021-04-17 10:31:29

- Part 1- Setting out and the great variable hunt
  
I have heard so much about the Python language over the last couple of years that I decided that it was time to see what it's really like. So I  found myself a nice course on the basics and whizzed through that. The course incidentally was [Learn Python - Full Course for Beginners on You Tube](https://www.youtube.com/watch?v=rfscVS0vtbw&t=9043s). It gave me a good grasp on the basics and showed me that a lot what I already knew was transferable across with the odd quirk. This made me feel all warm and fuzzy inside...so it was onwards and upwards to bigger and better things.

Now I really want to use Python to  work with the cloud providers, you know the type of thing I could use it for creating and managing environments through the use of function apps in azure or Lambda functions in AWS amongst other ways yet to be found, thought or dreamt about.

So I did the what I thought was the clever thing and jumped on the Microsoft Azure docs and typed "Manage Azure resources with Python", I got a bazillion choices but right there was this [one](https://docs.microsoft.com/en-us/samples/azure-samples/resource-manager-python-resources-and-groups/manage-azure-resources-and-resource-groups-with-python/) which seemed to tick all the right boxes, I didn't have to even write any code, great for dipping my toe in the water.
![The course](https://rgsnelson.co.uk/wp-content/uploads/2021/04/azurepythondoc.jpg)
So off I went working my way through the tutorial , So my computer of choice is a Mac on which I got to the point of running the  code after what I thought was cleverly following each step. I had to research one or two little steps along the way but I was going quickly and smoothly this is when over confidence reared its head. I arrived at the final stage I get to run the code, I excitedly entered the correct string shown in the tutorial and it failed.

I sat there, looking waiting for it to spark into life, but it doggedly stayed the same, I mean why wouldn't it, not only didn't the code work it couldn't work (this I found later). So I traced my steps and all looked well I had followed the steps as required. Was the doc out of date, checked that and nope it was good. So I did what all techies I didn't believe the computer and I ran it again, maybe I had caught the computer on a break, so the code again same outcome, nothing worked and I had the same error. I don't know what I had expected to change nothing had, it had just felt like the right thing to do to run the same code. I am sure some or all of you will have done this.

Turning to what was a more sensible idea I looked at the error message, hmm it says that there is something wrong with the Security Principal I had created or so the message hinted, in fact what the message said was that the security principal didn't exist.. Well it did I had proof I could see the accounts entry right there in the list of role assignments in Azure. Look for yourself... python_local in all its glory, user for Python to be used by my local machine...Snazzy name....I am sure you'll agree
![The Missing Account](https://rgsnelson.co.uk/wp-content/uploads/2021/04/firstsecurityprincipal.png)
Then I checked the GUID's etc in the error and....Errr  the GUID's not right the they don't match to the imaginatively named afore mentioned python_local where the hell is the script getting that GUID from. I must have got the Environment Variable wrong, so I jumped into the file I had put them in and there they were in all there glory exactly as I had put them  exported and everything. So being a good techie I double checked and tried again. This time surely it will work. Wrong.... where is that drawing board I need to start again.

Right I know Ill do a "printenv" very much like "set" on a windows machine it shows all environmental variables, so I typed in the command and they were.... NOT there. Aha so that was the problem. I had the syntax wrong... Google being my friend told me I had the syntax right. Humph!!!!  I mean I had created .bash_profile in the right place and the syntax was right. So sat there looking I see the type of shell I have is "zsh" not "bash". Then the light dawns on me Apple have changed the default shell to zsh from bash so .bash_profile is the wrong file. Yay!! I was on to something now.

<p align="center">
  <img width="315" height="215" src="https://rgsnelson.co.uk/wp-content/uploads/2021/04/zshshell_dev.png">
</p>

So what was the right file, I quickly looked in my users home folder and there was .zshrc I opened it up and it had the usual commands etc I would expect to see. So I transferred my variables to that. Tried my code again hopeful and confident of a good outcome and NOPE!! same error. Some bad language was uttered at this point which I won't pass on. I guess you can imagine some of what was said. So back to google, where oh where do I put the environmental variables for the ZSH shell.

I got the answer half way down a page on on stack overflow. There should be a file called .zshenv. I looked it made sense ZSH Environment shortened, That must be it. So I created the file moved my variables yet again. Restarted all my shells (there was one or two open by now) and ran "printenv". What Joy at seeing those 4 lovely variables. Now will my code work....I entered the command to start the python code. YES the lovely flow of information as my python ran and created and then deleted the a resource group. Yes just the one. Not much of a script to run but it ran all the same and as they say from small acorns do big oak trees grow.

I enjoyed the trouble shooting, its not brilliant when things don't work. I feel you learn most when this happens it also boosts your confidence when you solve the issue and can move on leaving triumph on your wake. It might have been a small script but it felt like a victory to me.