{"id":596,"date":"2019-12-06T08:49:47","date_gmt":"2019-12-06T08:49:47","guid":{"rendered":"https:\/\/www.triptera.com.au\/wordpress\/?p=596"},"modified":"2019-12-06T08:49:49","modified_gmt":"2019-12-06T08:49:49","slug":"using-pycharm-with-anaconda","status":"publish","type":"post","link":"https:\/\/www.triptera.com.au\/wordpress\/2019\/12\/06\/using-pycharm-with-anaconda\/","title":{"rendered":"Using PyCharm with Anaconda"},"content":{"rendered":"\n<p>There are a few tricks to getting PyCharm to work with Anaconda. This post demonstrates running a flask web application on Windows 10 with the only Python installation being Anaconda. The source code we will use is a git repository so you will need <code>git <\/code>installed to proceed. (see <a href=\"https:\/\/git-scm.com\">https:\/\/git-scm.com<\/a>). The screenshots use PyCharm Edu which is the same as PyCharm Community with the edu-tools plugin. You don&#8217;t need this plugin. Also, we do not have the Anaconda plugin which I think is only available for PyCharm Pro.<\/p>\n\n\n\n<p>To begin, run PyCharm. You may need to close any open projects to see the splash screen. (File menu > Close project). <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"779\" height=\"490\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img01-splash.png\" alt=\"\" class=\"wp-image-597\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img01-splash.png 779w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img01-splash-300x189.png 300w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img01-splash-768x483.png 768w\" sizes=\"auto, (max-width: 779px) 100vw, 779px\" \/><\/figure>\n\n\n\n<p>1. You can clone a git repository straight from the splash screen.  Click &#8220;Get from Version Control&#8221;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"779\" height=\"461\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img02-git-checkout.png\" alt=\"\" class=\"wp-image-598\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img02-git-checkout.png 779w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img02-git-checkout-300x178.png 300w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img02-git-checkout-768x454.png 768w\" sizes=\"auto, (max-width: 779px) 100vw, 779px\" \/><\/figure>\n\n\n\n<p>2.  Select &#8220;Repository URL&#8221; and fill in the details of the repo on github.<br \/>Version control: Git<br \/>URL: <a href=\"https:\/\/github.com\/timcu\/bpss-prime-minister\">https:\/\/github.com\/timcu\/bpss-prime-minister<\/a><br \/>Directory: C:\\Users\\<em>&lt;username><\/em>\\PyCharmProjects\\bpss-prime-minister <br \/>Click &#8220;Clone&#8221; to clone the repository<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"568\" height=\"110\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img03-open-directory.png\" alt=\"\" class=\"wp-image-599\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img03-open-directory.png 568w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img03-open-directory-300x58.png 300w\" sizes=\"auto, (max-width: 568px) 100vw, 568px\" \/><\/figure>\n\n\n\n<p>3. Click &#8220;Yes&#8221; to open the project directory in PyCharm.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"770\" height=\"338\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img04-settings.png\" alt=\"\" class=\"wp-image-600\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img04-settings.png 770w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img04-settings-300x132.png 300w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img04-settings-768x337.png 768w\" sizes=\"auto, (max-width: 770px) 100vw, 770px\" \/><\/figure>\n\n\n\n<p>4. Now you need to configure PyCharm to use an Anaconda virtual environment. This is done in &#8220;File&#8221; > &#8220;Settings&#8230;&#8221; in Windows\/Linux or &#8220;PyCharm&#8221; > &#8220;Preferences&#8221; in Mac.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"902\" height=\"701\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img05-show-all-interpreter.png\" alt=\"\" class=\"wp-image-601\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img05-show-all-interpreter.png 902w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img05-show-all-interpreter-300x233.png 300w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img05-show-all-interpreter-768x597.png 768w\" sizes=\"auto, (max-width: 902px) 100vw, 902px\" \/><\/figure>\n\n\n\n<p>5. In settings, select the Project Interpreter for this project. As this is the first project, no Interpreters are currently set up. Click on &#8220;Show All&#8230;&#8221; in the Project Interpreter pull down menu.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"359\" height=\"209\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img06-project-interpreters-add.png\" alt=\"\" class=\"wp-image-603\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img06-project-interpreters-add.png 359w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img06-project-interpreters-add-300x175.png 300w\" sizes=\"auto, (max-width: 359px) 100vw, 359px\" \/><\/figure>\n\n\n\n<p>6. The list of Project Interpreters is empty, so click on the &#8220;+&#8221; to create a new one.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"670\" height=\"320\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img07-add-python-interpreter.png\" alt=\"\" class=\"wp-image-604\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img07-add-python-interpreter.png 670w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img07-add-python-interpreter-300x143.png 300w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/figure>\n\n\n\n<p>7. Be sure to select &#8220;Conda Environment&#8221; in the left panel. If you were using Python without Anaconda you would select &#8220;Virtualenv Environment&#8221;. Selecting &#8220;System Interpreter&#8221; is not best practice because you will pollute the system python installation with custom modules making it difficult to replicate environments on different computers. &#8220;Pipenv Environment&#8221; is for those who prefer using <code>pipenv <\/code>to manage environments. This is more a legacy requirement as &#8220;Virtualenv Environment&#8221;, which these days uses <code>venv <\/code>rather than <code>virtualenv<\/code>, can do anything <code>pipenv <\/code>can do.<\/p>\n\n\n\n<p>Conda environments are stored in the Anaconda root directory. I installed Anaconda for my use only so this directory is <code>C:\\Users\\&lt;username>\\Anaconda3<\/code> . If you have installed Anaconda for all users it will probably be <code>C:\\Anaconda3<\/code> .<\/p>\n\n\n\n<p>The conda environment can be called anything you want but I normally call it the same name as the project so I know what it is used for. In this case I have named it &#8220;bpss-prime-minister&#8221;.<\/p>\n\n\n\n<p>Click &#8220;OK&#8221; to create the new conda environment. Then click &#8220;OK&#8221; on the &#8220;Project Interpreters&#8221; window to return to &#8220;Settings&#8221;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"902\" height=\"701\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img08-project-interpreter.png\" alt=\"\" class=\"wp-image-606\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img08-project-interpreter.png 902w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img08-project-interpreter-300x233.png 300w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img08-project-interpreter-768x597.png 768w\" sizes=\"auto, (max-width: 902px) 100vw, 902px\" \/><\/figure>\n\n\n\n<p>8. Click &#8220;OK&#8221; ensuring the newly created environment is now selected as the Project Interpreter in the pull down menu.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"764\" height=\"278\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img09-terminal.png\" alt=\"\" class=\"wp-image-607\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img09-terminal.png 764w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img09-terminal-300x109.png 300w\" sizes=\"auto, (max-width: 764px) 100vw, 764px\" \/><\/figure>\n\n\n\n<p>9. Open Terminal pane by clicking on &#8220;Terminal&#8221; in the bottom left of the project window.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"837\" height=\"388\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img10-install-requirements.png\" alt=\"\" class=\"wp-image-608\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img10-install-requirements.png 837w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img10-install-requirements-300x139.png 300w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img10-install-requirements-768x356.png 768w\" sizes=\"auto, (max-width: 837px) 100vw, 837px\" \/><\/figure>\n\n\n\n<p>10. In the future, when you open Terminal, it will automatically activate the correct conda environment. It may even activate it now as it has done for me in the screenshot. However, if you opened Terminal before creating the environment, it will be using base environment, indicated by <code>(base)<\/code> at the beginning of each prompt in Terminal. Activate the new conda environment by typing:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">conda activate bpss-prime-minister<\/pre>\n\n\n\n<p>After activating, each command line prompt should start with <code>(bpss-prime-minister)<\/code> .<\/p>\n\n\n\n<p>Now you need to install the library requirements which are stored in <code>requirements.txt<\/code>. Normally you would type <code>conda install --file requirements.txt<\/code> or even leave it for PyCharm to automatically install requirements, but in this case <code>conda <\/code>has problems installing <code>flask_bootstrap <\/code>and <code>python-dotenv<\/code>. To solve these issues I have decided to use <code>pip <\/code>to install all the requirements. PyCharm Terminal has already put me in the correct directory so I can just use the filename without the path.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pip install -r requirements.txt<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"837\" height=\"389\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img11-flask-run.png\" alt=\"\" class=\"wp-image-609\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img11-flask-run.png 837w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img11-flask-run-300x139.png 300w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img11-flask-run-768x357.png 768w\" sizes=\"auto, (max-width: 837px) 100vw, 837px\" \/><\/figure>\n\n\n\n<p>11. After all the requirements are installed you can launch the app. The first command is a custom command defined by the app to initialise the database. In Terminal type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">flask init-db<\/pre>\n\n\n\n<p>The second command launches the app in a dev only web server. In Terminal type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">flask run<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"854\" height=\"345\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img12-browser.png\" alt=\"\" class=\"wp-image-610\" srcset=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img12-browser.png 854w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img12-browser-300x121.png 300w, https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2019\/12\/img12-browser-768x310.png 768w\" sizes=\"auto, (max-width: 854px) 100vw, 854px\" \/><\/figure>\n\n\n\n<p>12. You can click on the displayed hyperlink in Terminal, or open a web browser such as Firefox and type in the address.<\/p>\n\n\n\n<p><a href=\"http:\/\/127.0.0.1:5000\">http:\/\/127.0.0.1:5000<\/a><\/p>\n\n\n\n<p>Congratulations. You are now running a flask web application on your Windows computer using only Anaconda and PyCharm. To stop the application, go back to Terminal and type ctrl-c, by holding down the &#8220;Ctrl&#8221; key, pressing the &#8220;c&#8221; key, and then releasing both keys.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a few tricks to getting PyCharm to work with Anaconda. This post demonstrates running a flask web application on Windows 10 with the only Python installation being Anaconda. The source code we will use is a git repository so you will need git installed to proceed. (see https:\/\/git-scm.com). The screenshots use PyCharm Edu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"categories":[17],"tags":[],"class_list":["post-596","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/comments?post=596"}],"version-history":[{"count":5,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/596\/revisions"}],"predecessor-version":[{"id":613,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/596\/revisions\/613"}],"wp:attachment":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/media?parent=596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/categories?post=596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/tags?post=596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}