Get Started Coding with Python: System Setup and Running Scripts
I’ve done a fair bit prior in PHP. I still love PHP because its easy to run, great for web and the syntax is straight forward, but I can’t deny that Python has a strangle hold on the network world so I’ve been moving most everything new I write to Python. Admittedly this has made passing scripts to other engineers for use easier.
Getting started is always the hardest. I’m sure there are similar posts on the internet to get started, but often I find that posts about “Doing X” only involve X and don’t talk about much else. This post is meant to help get you from not running Python to running Python the easiest way and in a way that will set you up to learn going forward. That means a couple of extra steps here and there and also leaning on Ubuntu (even in Windows) as the OS layer. Yes Python can be used across platforms, but with caveats. Simple things like using a “/” instead of a “\” will ruin your code. Linux on Windows is easy now, so why not just do it and be consistent. Also given my background this is more geared to “running scripts” than programing. I realize you’re not going to be writing an awesome GUI in Ubuntu on Windows and expecting it to run, but lets face it. The days of thick clients are over. If you need a GUI just run a web service and call it a day.
Phase 1 (or None): Install Ubuntu on Windows10
If you’re on Linux or Mac already then you can ignore this part. The first step is getting Ubuntu up and running in your Windows environment. Microsoft has made a huge u-turn over the last decade and is embracing Linux (with a big nod to Ubuntu). That means we can install the Windows Subsystem for Linux and then go to the Windows app store and just “get Ubuntu”. It’s kind of that easy (unless you’re fighting Windows updates for a while).
Speaking of Updates you need to be on Windows10 1909:
Once that is done we need to enable the Windows Subsystem for Linux from PowerShell, then reboot:
Now that the backend stuff is finished we just open the Microsoft App Store, find Ubuntu and install:
Once that is installed you can click the “Launch button” there or find Ubuntu in your installed apps: Launch it:
When you Launch Ubuntu for the first time you will need to setup an account. This is a Linux account and not a Windows account. It is a new account not an existing Windows account. They can be different (and in reality are different even if they’re the same. Make it easy on yourself and make it the same. But remember the password policies are not the same, because the accounts are not really the same. just use something you’ll remember…
And Now you’re in.
Phase 2 (or 1?): Getting Setup in Ubuntu
Now that you have Ubuntu or a fresh VM or machine or something running we need to get started. The first things are convenience:
Add yourself to the “admin” group so you don’t have to type your password every time you run something as “sudo” all the time (user be warned security/China/Russia etc)
sudo usermod -a -G admin <username>
Next turn on Auto-complete case insensitivity, because when you don’t really know what you’re looking for, knowing the case of that next letter is kind of not a given:
echo set completion-ignore-case on | sudo tee -a /etc/inputrc
And assuming you’ll be working on files on your desktop at some point, let’s make that easy to get to (If you didn’t make your username the same as your Windows user or something, just replace out “$USER” with whatever you need to make that work (Tab it) ). This will create a shortcut for your desktop in your Linux users home folder via a “link”. Rinse and repeat as you feel nessesary:
sudo ln -s /mnt/c/Users/$USER/Desktop ~/
You will have to close the console and open it again for most of these to be active.
Phase 3: Installing the Python stuff
So now the guts of what we need to do and one of the things that makes doing this in Linux so easy. Installing Python (and stuff) via apt-get
sudo apt-get update sudo apt-get install -y python3 python3-pip python3-virtualenv
And we’re done.
So we installed 3 things to get started. Python (obviously) in the 3.x “train”, pip which is a package manager for Python and makes installing libraries easy (which despite my disdain for tabs v spaces is probably the best thing Python has going for it) and virtual environments (more on that later)
Next we’re going to do some more things of convenience. We’re going to create a link of the python3 stuff to just plain ol python so you don’t have to remember python3 every time.:
sudo ln /usr/bin/python3 /usr/bin/python sudo ln /usr/bin/pip3 /usr/bin/pip
Just to make sure we are running you can open a python console. If it works great. Type “quit()” to get back out. If it doesn’t work… Google.
js@DESKTOP:~$ python Python 3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> quit() js@DESKTOP:~$
(No I did not cover getting this running on a Mac. I tried running a Mac for a couple of years, but switched back to an Ubuntu laptop. There’s lot of stuff on installing Python on a Mac using Brew, Go find it and then catch up later.)
Phase 4: Virtual Environments
Now to get started you caen just have at it and start writting and running scripts installing libraries etc, but I recommend getting used to virtual environments. So one of the claims of Python is how easy it is to just pick up a script and move it to any other box to run. That’s true, but what you don’t realize to start with is that your scripts are going to import all sorts of libraries to do all sorts of things (REST API, read/write to excel, etc etc). Those libraries need to be on the other machine too. Oh and those all need installed on the other box too and if the versions are the same results may vary.
Virtual Environments take a little more space and it is an extra step from “easy” but it will create a folder with all the “stuff” you need to pick up and move to a different machine including all your work files, the same version of Python and all the libraries you are using (in the same version). For mobility sake (and not to mention backups etc) virtual environments are great.
To create a new virtual environment (you can have as many as you want) use: the command “virtualenv <name>”. The command will create a new folder with the name that you specified and then create all the baseline files you need to get running. We can use “tree” (if installed) to see all the fancy new files.
js@DESKTOP:~$ virtualenv test2 created virtual environment CPython3.8.2.final.0-64 in 3215ms creator CPython3Posix(dest=/home/js/test2, clear=False, global=False) seeder FromAppData(download=False, appdirs=latest, CacheControl=latest, certifi=latest, chardet=latest, colorama=latest, contextlib2=latest, distlib=latest, distro=latest, html5lib=latest, idna=latest, ipaddr=latest, lockfile=latest, msgpack=latest, packaging=latest, pep517=latest, pip=latest, pkg_resources=latest, progress=latest, pyparsing=latest, pytoml=latest, requests=latest, retrying=latest, setuptools=latest, six=latest, urllib3=latest, webencodings=latest, wheel=latest, via=copy, app_data_dir=/home/jsnipes/.local/share/virtualenv/seed-app-data/v1.0.1.debian) activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator js@DESKTOP:~$ js@DESKTOP:~$ js@DESKTOP:~$ ls Desktop test2 js@DESKTOP:~$ tree test2 test2 ├── bin ├── lib ... ... │ │ └── top_level.txt │ └── wheel-0.34.2.virtualenv └── pyvenv.cfg 87 directories, 711 file
In order to start using it we need to activate it first. This command will put you into that virtual environment. Any libraries you install via pip, any scripts or base files and folders should be in this directory and then it can be picked up and moved anywhere. Here i’m activating the enviroment and install requests (already there) and xlsxwriter which lets you write to an Excell sheet.
js@DESKTOP:~$ source test2/bin/activate (test2) js@DESKTOP:~$ (test2) js@DESKTOP:~$ (test2) js@DESKTOP:~$ pip install requests xlsxwriter Requirement already satisfied: requests in ./test2/lib/python3.8/site-packages (2.22.0) Collecting xlsxwriter Using cached XlsxWriter-1.3.4-py2.py3-none-any.whl (144 kB) Installing collected packages: xlsxwriter Successfully installed xlsxwriter-1.3.4 (test2) js@DESKTOP:~$
Phase 5: Hello World!
Now we are ready to write code. Remember to work in the correct folder so cd (change directories) to your new virtual environment (if you need to) and get to work. “nano” is a great tool availble in Linux that is a easy dirty command line text editor. You can also edit via vi or what in the Linux terminal or use Notepad++ etc in windows and just run it from Ubuntu. “cat” is also a the Linux command to display a file in the terminate.
(test2) js@DESKTOP:~$ cd test2 (test2) js@DESKTOP:~/test2$ nano test.py --Did Stuff-- (test2) js@DESKTOP:~/test2$ cat test.py #! /usr/bin/python3 print("Hello World!") (test2) js@DESKTOP:~/test2$
Now that we have our program we an just reference it through Python command or we can make it executable and run it direct. To leave the virtual environment when you are done just say “deactivate”
(test2) js@DESKTOP:~/test2$ python3 test.py Hello World! -OR- (test2) js@DESKTOP:~/test2$ chmod +x test.py (test2) js@DESKTOP:~/test2$ ./test.py Hello World! (test2) js@DESKTOP:~/test2$ (test2) js@DESKTOP:~/test2$ deactivate js@DESKTOP:~/test2$
Bonus note: You can run the script without explicitly dropping into the virtual environment with a little help. Outside of your virtual environment, use pip to install venv-run. When you call this command and the script it will automatically enter the virtual environment for you and run it from there.
js@DESKTOP-:~$ pip3 install venv-run Collecting venv-run Using cached venv_run-0.0.0-py3-none-any.whl (7.3 kB) Installing collected packages: venv-run WARNING: The script venv-run is installed in '/home/jsnipes/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. Successfully installed venv-run-0.0.0 js@DESKTOP-:~$ js@DESKTOP-:~$ PATH=$PATH:/home/jsnipes/.local/bin js@DESKTOP-:~$ js@DESKTOP-:~$ venv-run test2/test.py Hello World! js@DESKTOP-:~$