{"id":16632,"date":"2019-12-23T16:29:09","date_gmt":"2019-12-24T00:29:09","guid":{"rendered":"http:\/\/compositecode.blog\/?p=16632"},"modified":"2020-04-16T09:16:51","modified_gmt":"2020-04-16T16:16:51","slug":"getting-started-with-python-right","status":"publish","type":"post","link":"https:\/\/compositecode.blog\/2019\/12\/23\/getting-started-with-python-right\/","title":{"rendered":"Getting Started With Python Right!"},"content":{"rendered":"<blockquote><p><a href=\"https:\/\/compositecode.blog\/2019\/12\/25\/unbreaking-python-through-virtual-environments\/\">UPDATE POST<\/a>\u00a0Added details about virtualenv to complement this post, per Thomas&#8217; comment in the comments section below! Read this post for MacOS specific system Python setup, then read that for more options on how to set things up right!<\/p><\/blockquote>\n<p>Recently I sat down to get started on some Python work, specifically on MacOS. I&#8217;ve written code in Python before and tried out 2.x and 3.x before. I&#8217;m going with 3.x for this and upcoming articles but one thing became apparent. The Python tech stack is still ferociously fragmented in a number of ways, and this post is to provide some clarity if this is your first endeavor into the stack.<!--more--><\/p>\n<p>First off, 2.7 and 3.8 are the versions that exist in some state as the\u00a0<em>latest versions<\/em>. 2.7 is obviously not the latest but is still installed, coded in, and used in a vast number of projects where Python is used. On MacOS for example, it&#8217;s the default still, and it should be left alone and the latest installed with appropriate Python environment, PIP for dependency, and related ecosystem necessities. As I walk through these steps, that&#8217;s important to know.<\/p>\n<p>Whatever the system environment starting, check out what is on the machine. If on Windows, it&#8217;s safe to bet there is nothing on the machine. On Linux or MacOS there will be some version of Python already installed. Running\u00a0<code>python --version<\/code>\u00a0to determine which version of the language is installed.<\/p>\n<p>If you have a version you can then check out\u00a0<code>which python<\/code>\u00a0to figure out where it is installed. This can come in handy if things get a bit confusing, or verifying where the install is currently setup or pointing to within the file structure.<\/p>\n<h2><a class=\"anchor\" href=\"https:\/\/dev.to\/adron\/getting-started-with-python-right-10k#first-step-get-a-tool-to-install-python\" name=\"first-step-get-a-tool-to-install-python\"><\/a>First Step: Get a tool to install Python<\/h2>\n<p>On MacOS get\u00a0<a href=\"https:\/\/brew.sh\/\">brew<\/a>\u00a0before doing anything else. For details on Windows check out the\u00a0<a href=\"https:\/\/docs.python.org\/3\/using\/windows.html\">docs here<\/a>. There&#8217;s also a great Digital Ocean post by Lisa Tagliaferri &#8220;<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-install-python-3-and-set-up-a-local-programming-environment-on-windows-10\">How to Install python 3 and Set Up a Local Programming Environment on Windows 10<\/a>&#8221; if you&#8217;re installig Python 3 on Windows 10. For Linux specifically check out these\u00a0<a href=\"https:\/\/docs.python.org\/3\/using\/unix.html\">docs<\/a>.<\/p>\n<p>Once brew is installed issue the command to install the &#8220;Simple Python Version Management&#8221;\u00a0<a href=\"https:\/\/github.com\/pyenv\/pyenv\">pyenv<\/a>. If this app gives flashbacks to Ruby, it is indeed forked from\u00a0<a href=\"https:\/\/github.com\/rbenv\/rbenv\">rbenv<\/a>\u00a0so there&#8217;s that.<\/p>\n<div class=\"highlight\">\n<pre class=\"highlight plaintext\"><code>brew install pyenv\n<\/code><\/pre>\n<\/div>\n<h2><a class=\"anchor\" href=\"https:\/\/dev.to\/adron\/getting-started-with-python-right-10k#second-step-install-the-latest-python\" name=\"second-step-install-the-latest-python\"><\/a>Second Step: Install the Latest Python<\/h2>\n<p>With pyenv install a sepcific version of Python with the following command.<\/p>\n<div class=\"highlight\">\n<pre class=\"highlight plaintext\"><code>pyenv install 3.8.0\n<\/code><\/pre>\n<\/div>\n<p>If there is a new version, install that version instead. On MacOS there is also, of course, already version 2.7 but with pyenv it&#8217;ll get the appropriate pieces remapped so that this latest can become the defacto used Python version to work with. To get pyenv to setup this latest installed version us the CLI to set it as the global Python.<\/p>\n<div class=\"highlight\">\n<pre class=\"highlight plaintext\"><code>pyenv global 3.8.0\n<\/code><\/pre>\n<\/div>\n<p>One final step to ensure pyenv can be used when and where necessary, now and ongoing in development needs, set the following in your .zshrc.<\/p>\n<div class=\"highlight\">\n<pre class=\"highlight plaintext\"><code>echo -e 'if command -v pyenv 1&gt;\/dev\/null 2&gt;&amp;1; then\\n  eval \"$(pyenv init -)\"\\nfi' &gt;&gt; ~\/.zshrc\n<\/code><\/pre>\n<\/div>\n<p>If you&#8217;re using bash still, make the change accordingly to your\u00a0<code>~\/.bash_profile<\/code>.<\/p>\n<div class=\"highlight\">\n<pre class=\"highlight plaintext\"><code>echo -e 'if command -v pyenv 1&gt;\/dev\/null 2&gt;&amp;1; then\\n  eval \"$(pyenv init -)\"\\nfi' &gt;&gt; ~\/.bash_profile\n<\/code><\/pre>\n<\/div>\n<h2><a class=\"anchor\" href=\"https:\/\/dev.to\/adron\/getting-started-with-python-right-10k#third-step-get-pip-installed\" name=\"third-step-get-pip-installed\"><\/a>Third Step: Get PIP Installed.<\/h2>\n<p>To check the version of\u00a0<code>pip<\/code>\u00a0run\u00a0<code>pip -V<\/code>. This command not only shows you which version of pip is installed, but also what version of Python pip is installed to oeprate on (with\/on\/at\/to??). The command and result would look something like this.<\/p>\n<div class=\"highlight\">\n<pre class=\"highlight plaintext\"><code>pip -V\npip 19.0.3 from \/Users\/adron\/.pyenv\/versions\/3.7.3\/lib\/python3.7\/site-packages\/pip (python 3.7)\n<\/code><\/pre>\n<\/div>\n<p>Technically, when brew (Homebrew) installed the latest Python has also installed pip and it should be mapped accordingly as shown above in the\u00a0<code>pip -V<\/code>\u00a0command. However this can be set correctly if it isn&#8217;t mapped already. Get the path where pip3 is installed by issuing a which command.<\/p>\n<div class=\"highlight\">\n<pre class=\"highlight plaintext\"><code>which pip3\n<\/code><\/pre>\n<\/div>\n<p>The path returned, likely\u00a0<code>\/usr\/local\/bin\/pip3<\/code>, add it to a command to echo it into the startup script of your choice. For example, to add it to the zshrc file.<\/p>\n<div class=\"highlight\">\n<pre class=\"highlight plaintext\"><code>echo \"alias pip=\/usr\/local\/bin\/pip3\" &gt;&gt; ~\/.zshrc \n<\/code><\/pre>\n<\/div>\n<p>If using bash add it as such.<\/p>\n<div class=\"highlight\">\n<pre class=\"highlight plaintext\"><code>echo \"alias pip=\/usr\/local\/bin\/pip3\" &gt;&gt; ~\/.bash_profile\n<\/code><\/pre>\n<\/div>\n<h2><a class=\"anchor\" href=\"https:\/\/dev.to\/adron\/getting-started-with-python-right-10k#fourth-step-run-some-python-code\" name=\"fourth-step-run-some-python-code\"><\/a>Fourth Step: Run Some Python Code!<\/h2>\n<p>Alright, now that this is done, let&#8217;s run some actual Python code and make sure things are installed and setup correctly. For this I created a directory I simply called\u00a0<code>python-examples.py<\/code>. Then added the first line of code, a simple print statement.<\/p>\n<div class=\"highlight\">\n<pre class=\"highlight python\"><code><span class=\"k\">print<\/span><span class=\"p\">(<\/span><span class=\"s\">\"Hello World! BOOM!\"<\/span><span class=\"p\">)<\/span>\n\n<span class=\"k\">print<\/span><span class=\"p\">(<\/span><span class=\"s\">\"These examples point out how these variables can be declared and printed.\"<\/span><span class=\"p\">)<\/span>\n<\/code><\/pre>\n<\/div>\n<p>Executing that, just by executing the file\u00a0<code>python python_examples.py<\/code>\u00a0and the following results display.<\/p>\n<div class=\"highlight\">\n<pre class=\"highlight plaintext\"><code>Hello World! BOOM!\nThese examples point out how these variables can be declared and printed.\n<\/code><\/pre>\n<\/div>\n<p>Alright, with that confirmed, we&#8217;re all set for further Python development! Enjoy!<\/p>\n<h4><a class=\"anchor\" href=\"https:\/\/dev.to\/adron\/getting-started-with-python-right-10k#references\" name=\"references\"><\/a>References:<\/h4>\n<ul>\n<li><a href=\"https:\/\/docs.python.org\/3\/\">Python Docs<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/3\/tutorial\/index.html\">Python v3 Tutorial<\/a><\/li>\n<li>Not discussed here, but I love the JetBrains IDE&#8217;s, and they&#8217;ve got one for Python called\u00a0<a href=\"https:\/\/www.jetbrains.com\/pycharm\/\">PyCharm<\/a>.<\/li>\n<li>More details about setting up and other ways to put things together by Matthew Broberg\u00a0<a href=\"https:\/\/twitter.com\/mbbroberg\">@mbbroberg<\/a>\u00a0and Moshe Zadka\u00a0<a href=\"https:\/\/twitter.com\/moshezadka\">@moshezadka<\/a>\u00a0in the post &#8220;<a href=\"https:\/\/opensource.com\/article\/19\/5\/python-3-default-mac\">The Right and Wrong Way to Set Python 3 as Default on the Mac<\/a>&#8220;.<\/li>\n<\/ul>\n<p>My Blog! I have a personal blog at\u00a0<a href=\"https:\/\/compositecode.blog\/\">https:\/\/compositecode.blog\/<\/a>\u00a0that you can also subscribe to where I post additional collected material, thoughts, music, meetups, conferences, videos (like\u00a0<a href=\"https:\/\/twitch.tv\/adronhall\/\">Twitch<\/a>), open source projects, database work, data science, and much more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve gone through a gazillion ways to setup Python, ensure I&#8217;ve gotten the right version installed on MacOS. This is a quick guide I&#8217;ve put together if you&#8217;re getting started or looking at a more defacto set way to get it installed. I&#8217;ve added links for Linux and Windows too, but specifically cover MacOS in this post.<\/p>\n","protected":false},"author":1272761,"featured_media":16633,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_crdt_document":"","advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false},"categories":[694746336],"tags":[91938,196,832,2932],"class_list":["post-16632","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-beginners","tag-programming","tag-python","tag-tutorial"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/compositecode.blog\/wp-content\/uploads\/2019\/12\/thrashing-code-python.jpg?fit=920%2C920&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pXk7p-4kg","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/compositecode.blog\/wp-json\/wp\/v2\/posts\/16632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/compositecode.blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/compositecode.blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/compositecode.blog\/wp-json\/wp\/v2\/users\/1272761"}],"replies":[{"embeddable":true,"href":"https:\/\/compositecode.blog\/wp-json\/wp\/v2\/comments?post=16632"}],"version-history":[{"count":5,"href":"https:\/\/compositecode.blog\/wp-json\/wp\/v2\/posts\/16632\/revisions"}],"predecessor-version":[{"id":16812,"href":"https:\/\/compositecode.blog\/wp-json\/wp\/v2\/posts\/16632\/revisions\/16812"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/compositecode.blog\/wp-json\/wp\/v2\/media\/16633"}],"wp:attachment":[{"href":"https:\/\/compositecode.blog\/wp-json\/wp\/v2\/media?parent=16632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/compositecode.blog\/wp-json\/wp\/v2\/categories?post=16632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/compositecode.blog\/wp-json\/wp\/v2\/tags?post=16632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}