One place for hosting & domains

      Python

      How To Install Python 3 and Set Up a Programming Environment on Debian 9


      Introduction

      A flexible and versatile programming language, Python is effective for many use cases, including scripting, automation, data analysis, machine learning, and back-end development. First published in 1991 with a name inspired by the British comedy group Monty Python, the development team wanted to make Python a language that was fun to use. Quick to set up, and written in a relatively straightforward style with immediate feedback on errors, Python is a great choice for beginners and experienced developers alike. Python 3 is the most current version of the language and is considered to be the future of Python.

      This tutorial will get your Debian 9 server set up with a Python 3 programming environment. Programming on a server has many advantages and supports collaboration across development projects.

      Prerequisites

      In order to complete this tutorial, you should have a non-root user with sudo privileges on a Debian 9 server. To learn how to achieve this setup, follow our Debian 9 initial server setup guide.

      If you’re not already familiar with a terminal environment, you may find the article “An Introduction to the Linux Terminal” useful for becoming better oriented with the terminal.

      With your server and user set up, you are ready to begin.

      Step 1 — Setting Up Python 3

      Debian Linux ships with both Python 3 and Python 2 pre-installed. To make sure that our versions are up-to-date, let’s update and upgrade the system with the apt command to work with the Advanced Packaging Tool:

      • sudo apt update
      • sudo apt -y upgrade

      The -y flag will confirm that we are agreeing for all items to be installed.

      Once the process is complete, we can check the version of Python 3 that is installed in the system by typing:

      You’ll receive output in the terminal window that will let you know the version number. While this number may vary, the output will be similar to this:

      Output

      Python 3.5.3

      To manage software packages for Python, let’s install pip, a tool that will install and manage programming packages we may want to use in our development projects. You can learn more about modules or packages that you can install with pip by reading “How To Import Modules in Python 3.”

      • sudo apt install -y python3-pip

      Python packages can be installed by typing:

      • pip3 install package_name

      Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install NumPy, you can do so with the command pip3 install numpy.

      There are a few more packages and development tools to install to ensure that we have a robust set-up for our programming environment:

      • sudo apt install build-essential libssl-dev libffi-dev python3-dev

      Once Python is set up, and pip and other tools are installed, we can set up a virtual environment for our development projects.

      Step 2 — Setting Up a Virtual Environment

      Virtual environments enable you to have an isolated space on your server for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.

      Setting up a programming environment provides us with greater control over our Python projects and over how different versions of packages are handled. This is especially important when working with third-party packages.

      You can set up as many Python programming environments as you want. Each environment is basically a directory or folder on your server that has a few scripts in it to make it act as an environment.

      While there are a few ways to achieve a programming environment in Python, we’ll be using the venv module here, which is part of the standard Python 3 library. Let’s install venv by typing:

      • sudo apt install -y python3-venv

      With this installed, we are ready to create environments. Let’s either choose which directory we would like to put our Python programming environments in, or create a new directory with mkdir, as in:

      • mkdir environments
      • cd environments

      Once you are in the directory where you would like the environments to live, you can create an environment by running the following command:

      Essentially, pyvenv sets up a new directory that contains a few items which we can view with the ls command:

      Output

      bin include lib lib64 pyvenv.cfg share

      Together, these files work to make sure that your projects are isolated from the broader context of your local machine, so that system files and project files don’t mix. This is good practice for version control and to ensure that each of your projects has access to the particular packages that it needs. Python Wheels, a built-package format for Python that can speed up your software production by reducing the number of times you need to compile, will be in the Ubuntu 18.04 share directory.

      To use this environment, you need to activate it, which you can achieve by typing the following command that calls the activate script:

      • source my_env/bin/activate

      Your command prompt will now be prefixed with the name of your environment, in this case it is called my_env. Depending on what version of Debian Linux you are running, your prefix may appear somewhat differently, but the name of your environment in parentheses should be the first thing you see on your line:

      This prefix lets us know that the environment my_env is currently active, meaning that when we create programs here they will use only this particular environment’s settings and packages.

      Note: Within the virtual environment, you can use the command python instead of python3, and pip instead of pip3 if you would prefer. If you use Python 3 on your machine outside of an environment, you will need to use the python3 and pip3 commands exclusively.

      After following these steps, your virtual environment is ready to use.

      Step 3 — Creating a “Hello, World” Program

      Now that we have our virtual environment set up, let’s create a traditional “Hello, World!” program. This will let us test our environment and provides us with the opportunity to become more familiar with Python if we aren’t already.

      To do this, we’ll open up a command-line text editor such as nano and create a new file:

      Once the text file opens up in the terminal window we’ll type out our program:

      print("Hello, World!")
      

      Exit nano by typing the CTRL and X keys, and when prompted to save the file press y.

      Once you exit out of nano and return to your shell, let’s run the program:

      The hello.py program that you just created should cause your terminal to produce the following output:

      Output

      Hello, World!

      To leave the environment, simply type the command deactivate and you will return to your original directory.

      Conclusion

      Congratulations! At this point you have a Python 3 programming environment set up on your Debian 9 Linux server and you can now begin a coding project!

      If you are using a local machine rather than a server, refer to the tutorial that is relevant to your operating system in our “How To Install and Set Up a Local Programming Environment for Python 3” series.

      With your server ready for software development, you can continue to learn more about coding in Python by reading our free How To Code in Python 3 eBook, or consulting our Programming Project tutorials.

      Download our free Python eBook!

      How To Code in Python eBook in EPUB format

      How To Code in Python eBook in PDF format



      Source link

      How To Install the Anaconda Python Distribution on Debian 9


      Introduction

      Anaconda is an open-source package manager, environment manager, and distribution of the Python and R programming languages. Designed for data science and machine learning workflows, it is commonly used for large-scale data processing, scientific computing, and predictive analytics.

      Available in both free and paid enterprise versions, Anaconda offers a collection of over 1,000 data science packages. The Anaconda distribution ships with the conda command-line utility. You can learn more about Anaconda and conda by reading the official Anaconda Documentation.

      This tutorial will guide you through installing the Python 3 version of Anaconda on a Debian 9 server.

      Prerequisites

      Before you begin with this guide, you should have a non-root user with sudo privileges set up on your server.

      You can achieve this prerequisite by completing our Debian 9 initial server setup guide.

      Installing Anaconda

      The best way to install Anaconda is to download the latest Anaconda installer bash script, verify it, and then run it.

      Find the latest version of Anaconda for Python 3 at the Downloads page accessible via the Anaconda home page. At the time of writing, the latest version is 5.2, but you should use a later stable version if it is available.

      Next, change to the /tmp directory on your server. This is a good directory to download ephemeral items, like the Anaconda bash script, which we won’t need after running it.

      We’ll use the curl command-line tool to download the script. Install curl:

      Now, use curl to download the link that you copied from the Anaconda website:

      • curl -O https://repo.anaconda.com/archive/Anaconda3-5.2.0-Linux-x86_64.sh

      We can now verify the data integrity of the installer with cryptographic hash verification through the SHA-256 checksum. We’ll use the sha256sum command along with the filename of the script:

      • sha256sum Anaconda3-5.2.0-Linux-x86_64.sh

      You’ll receive output that looks similar to this:

      Output

      09f53738b0cd3bb96f5b1bac488e5528df9906be2480fe61df40e0e0d19e3d48 Anaconda3-5.2.0-Linux-x86_64.sh

      You should check the output against the hashes available at the Anaconda with Python 3 on 64-bit Linux page for your appropriate Anaconda version. As long as your output matches the hash displayed in the sha2561 row, you’re good to go.

      Now we can run the script:

      • bash Anaconda3-5.2.0-Linux-x86_64.sh

      You’ll receive the following output:

      Output

      Welcome to Anaconda3 5.2.0 In order to continue the installation process, please review the license agreement. Please, press ENTER to continue >>>

      Press ENTER to continue and then press ENTER to read through the license. Once you’re done reading the license, you’ll be prompted to approve the license terms:

      Output

      Do you approve the license terms? [yes|no]

      As long as you agree, type yes.

      At this point, you’ll be prompted to choose the location of the installation. You can press ENTER to accept the default location, or specify a different location to modify it.

      Output

      Anaconda3 will now be installed into this location: /home/sammy/anaconda3 - Press ENTER to confirm the location - Press CTRL-C to abort the installation - Or specify a different location below [/home/sammy/anaconda3] >>>

      The installation process will continue. Note that it may take some time.

      Once installation is complete, you’ll receive the following output:

      Output

      ... installation finished. Do you wish the installer to prepend the Anaconda3 install location to PATH in your /home/sammy/.bashrc ? [yes|no] [no] >>>

      Type yes so that you can use the conda command. You’ll receive the following output next:

      Output

      Appending source /home/sammy/anaconda3/bin/activate to /home/sammy/.bashrc A backup will be made to: /home/sammy/.bashrc-anaconda3.bak ...

      Finally, you’ll receive the following prompt regarding whether or not you would like to download Visual Studio Code (or VSCode), a free and open-source editor for code developed by Microsoft that can run on Linux. You can learn more about the editor on the official Visual Studio Code website.

      At this point, you can decide whether or not to download the editor now by typing yes or no.

      Anaconda is partnered with Microsoft! Microsoft VSCode is a streamlined
      code editor with support for development operations like debugging, task
      running and version control.
      
      To install Visual Studio Code, you will need:
        - Administrator Privileges
        - Internet connectivity
      
      Visual Studio Code License: https://code.visualstudio.com/license
      
      Do you wish to proceed with the installation of Microsoft VSCode? [yes|no]
      >>> 
      

      In order to activate the installation, you should source the ~/.bashrc file:

      Once you have done that, you can verify your install by making use of the conda command, for example with list:

      You’ll receive output of all the packages you have available through the Anaconda installation:

      Output

      # packages in environment at /home/sammy/anaconda3: # # Name Version Build Channel _ipyw_jlab_nb_ext_conf 0.1.0 py36he11e457_0 alabaster 0.7.10 py36h306e16b_0 anaconda 5.2.0 py36_3 ...

      Now that Anaconda is installed, we can go on to setting up Anaconda environments.

      Setting Up Anaconda Environments

      Anaconda virtual environments allow you to keep projects organized by Python versions and packages needed. For each Anaconda environment you set up, you can specify which version of Python to use and can keep all of your related programming files together within that directory.

      First, we can check to see which versions of Python are available for us to use:

      You’ll receive output with the different versions of Python that you can target, including both Python 3 and Python 2 versions. Since we are using the Anaconda with Python 3 in this tutorial, you will have access only to the Python 3 versions of packages.

      Let’s create an environment using the most recent version of Python 3. We can achieve this by assigning version 3 to the python argument. We’ll call the environment my_env, but you’ll likely want to use a more descriptive name for your environment especially if you are using environments to access more than one version of Python.

      • conda create --name my_env python=3

      We’ll receive output with information about what is downloaded and which packages will be installed, and then be prompted to proceed with y or n. As long as you agree, type y.

      The conda utility will now fetch the packages for the environment and let you know when it’s complete.

      You can activate your new environment by typing the following:

      With your environment activated, your command prompt prefix will change:

      Within the environment, you can verify that you’re using the version of Python that you had intended to use:

      Output

      Python 3.7.0 :: Anaconda, Inc.

      When you’re ready to deactivate your Anaconda environment, you can do so by typing:

      Note that you can replace the word source with . to achieve the same results.

      To target a more specific version of Python, you can pass a specific version to the python argument, like 3.5, for example:

      • conda create -n my_env35 python=3.5

      You can update your version of Python along the same branch (as in updating Python 3.5.1 to Python 3.5.2) within a respective environment with the following command:

      If you would like to target a more specific version of Python, you can pass that to the python argument, as in python=3.3.2.

      You can inspect all of the environments you have set up with this command:

      Output

      # conda environments: # base * /home/sammy/anaconda3 my_env /home/sammy/anaconda3/envs/my_env my_env35 /home/sammy/anaconda3/envs/my_env35

      The asterisk indicates the current active environment.

      Each environment you create with conda create will come with several default packages:

      • openssl
      • pip
      • python
      • readline
      • setuptools
      • sqlite
      • tk
      • wheel
      • xz
      • zlib

      You can add additional packages, such as numpy for example, with the following command:

      • conda install --name my_env35 numpy

      If you know you would like a numpy environment upon creation, you can target it in your conda create command:

      • conda create --name my_env python=3 numpy

      If you are no longer working on a specific project and have no further need for the associated environment, you can remove it. To do so, type the following:

      • conda remove --name my_env35 --all

      Now, when you type the conda info --envs command, the environment that you removed will no longer be listed.

      Updating Anaconda

      You should regularly ensure that Anaconda is up-to-date so that you are working with all the latest package releases.

      To do this, you should first update the conda utility:

      When prompted to do so, type y to proceed with the update.

      Once the update of conda is complete, you can update the Anaconda distribution:

      Again when prompted to do so, type y to proceed.

      This will ensure that you are using the latest releases of conda and Anaconda.

      Uninstalling Anaconda

      If you are no longer using Anaconda and find that you need to uninstall it, you should start with the anaconda-clean module, which will remove configuration files for when you uninstall Anaconda.

      • conda install anaconda-clean

      Type y when prompted to do so.

      Once it is installed, you can run the following command. You will be prompted to answer y before deleting each one. If you would prefer not to be prompted, add --yes to the end of your command:

      anaconda-clean
      

      This will also create a backup folder called .anaconda_backup in your home directory:

      Output

      Backup directory: /home/sammy/.anaconda_backup/2018-09-06T183049

      You can now remove your entire Anaconda directory by entering the following command:

      Finally, you can remove the PATH line from your .bashrc file that Anaconda added. To do so, first open a text editor such as nano:

      Then scroll down to the end of the file (if this is a recent install) or type CTRL + W to search for Anaconda. Delete or comment out the export PATH line:

      /home/sammy/.bashrc

      ...
      # added by Anaconda3 installer
      export PATH="/home/sammy/anaconda3/bin:$PATH"
      

      When you’re done editing the file, type CTRL + X to exit and y to save changes.

      Anaconda is now removed from your server.

      Conclusion

      This tutorial walked you through the installation of Anaconda, working with the conda command-line utility, setting up environments, updating Anaconda, and deleting Anaconda if you no longer need it.

      You can use Anaconda to help you manage workloads for data science, scientific computing, analytics, and large-scale data processing. From here, you can check out our tutorials on data analysis and machine learning to learn more about various tools available to use and projects that you can do.



      Source link

      How To Set Up a Jupyter Notebook with Python 3 on Debian 9


      Introduction

      Jupyter Notebook offers a command shell for interactive computing as a web application. The tool can be used with several languages, including Python, Julia, R, Haskell, and Ruby. It is often used for working with data, statistical modeling, and machine learning.

      This tutorial will walk you through setting up Jupyter Notebook to run from a Debian 9 server, as well as teach you how to connect to and use the notebook. Jupyter notebooks (or simply notebooks) are documents produced by the Jupyter Notebook app which contain both computer code and rich text elements (paragraph, equations, figures, links, etc.) which aid in presenting and sharing reproducible research.

      By the end of this guide, you will be able to run Python 3 code using Jupyter Notebook running on a remote server.

      Prerequisites

      In order to complete this guide, you should have a fresh Debian 9 server instance with a basic firewall and a non-root user with sudo privileges configured. You can learn how to set this up by running through our Initial Server Setup with Debian 9 guide.

      Step 1 — Install Pip and Python Headers

      To begin the process, we’ll download and install all of the items we need from the Debian repositories. We will use the Python package manager pip to install additional components a bit later.

      We first need to update the local apt package index and then download and install the packages:

      Next, install pip and the Python header files, which are used by some of Jupyter’s dependencies:

      • sudo apt install python3-pip python3-dev

      Debian 9 (“Stretch”) comes preinstalled with Python 3.5.

      We can now move on to setting up a Python virtual environment into which we’ll install Jupyter.

      Step 2 — Create a Python Virtual Environment for Jupyter

      Now that we have Python 3, its header files, and pip ready to go, we can create a Python virtual environment for easier management. We will install Jupyter into this virtual environment.

      To do this, we first need access to the virtualenv command. We can install this with pip.

      Upgrade pip and install the package by typing:

      • sudo -H pip3 install --upgrade pip
      • sudo -H pip3 install virtualenv

      With virtualenv installed, we can start forming our environment. Create and move into a directory where we can keep our project files:

      • mkdir ~/myprojectdir
      • cd ~/myprojectdir

      Within the project directory, create a Python virtual environment by typing:

      This will create a directory called myprojectenv within your myprojectdir directory. Inside, it will install a local version of Python and a local version of pip. We can use this to install and configure an isolated Python environment for Jupyter.

      Before we install Jupyter, we need to activate the virtual environment. You can do that by typing:

      • source myprojectenv/bin/activate

      Your prompt should change to indicate that you are now operating within a Python virtual environment. It will look something like this: (myprojectenv)user@host:~/myprojectdir$.

      You’re now ready to install Jupyter into this virtual environment.

      Step 3 — Install Jupyter

      With your virtual environment active, install Jupyter with the local instance of pip:

      Note: When the virtual environment is activated (when your prompt has (myprojectenv) preceding it), use pip instead of pip3, even if you are using Python 3. The virtual environment's copy of the tool is always named pip, regardless of the Python version.

      At this point, you’ve successfully installed all the software needed to run Jupyter. We can now start the notebook server.

      Step 4 — Run Jupyter Notebook

      You now have everything you need to run Jupyter Notebook! To run it, execute the following command:

      A log of the activities of the Jupyter Notebook will be printed to the terminal. When you run Jupyter Notebook, it runs on a specific port number. The first notebook you run will usually use port 8888. To check the specific port number Jupyter Notebook is running on, refer to the output of the command used to start it:

      Output

      [I 21:23:21.198 NotebookApp] Writing notebook server cookie secret to /run/user/1001/jupyter/notebook_cookie_secret [I 21:23:21.361 NotebookApp] Serving notebooks from local directory: /home/sammy/myprojectdir [I 21:23:21.361 NotebookApp] The Jupyter Notebook is running at: [I 21:23:21.361 NotebookApp] http://localhost:8888/?token=1fefa6ab49a498a3f37c959404f7baf16b9a2eda3eaa6d72 [I 21:23:21.361 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [W 21:23:21.361 NotebookApp] No web browser found: could not locate runnable browser. [C 21:23:21.361 NotebookApp] Copy/paste this URL into your browser when you connect for the first time, to login with a token: http://localhost:8888/?token=1fefa6ab49a498a3f37c959404f7baf16b9a2eda3eaa6d72

      If you are running Jupyter Notebook on a local Debian computer (not on a Droplet), you can simply navigate to the displayed URL to connect to Jupyter Notebook. If you are running Jupyter Notebook on a Droplet, you will need to connect to the server using SSH tunneling as outlined in the next section.

      At this point, you can keep the SSH connection open and keep Jupyter Notebook running or can exit the app and re-run it once you set up SSH tunneling. Let's keep it simple and stop the Jupyter Notebook process. We will run it again once we have SSH tunneling working. To stop the Jupyter Notebook process, press CTRL+C, type Y, and hit ENTER to confirm. The following will be displayed:

      Output

      [C 21:28:28.512 NotebookApp] Shutdown confirmed [I 21:28:28.512 NotebookApp] Shutting down 0 kernels

      We’ll now set up an SSH tunnel so that we can access the notebook.

      Step 5 — Connect to the Server Using SSH Tunneling

      In this section we will learn how to connect to the Jupyter Notebook web interface using SSH tunneling. Since Jupyter Notebook will run on a specific port on the server (such as :8888, :8889 etc.), SSH tunneling enables you to connect to the server’s port securely.

      The next two subsections describe how to create an SSH tunnel from 1) a Mac or Linux and 2) Windows. Please refer to the subsection for your local computer.

      SSH Tunneling with a Mac or Linux

      If you are using a Mac or Linux, the steps for creating an SSH tunnel are similar to using SSH to log in to your remote server, except that there are additional parameters in the ssh command. This subsection will outline the additional parameters needed in the ssh command to tunnel successfully.

      SSH tunneling can be done by running the following SSH command in a new local terminal window:

      • ssh -L 8888:localhost:8888 your_server_username@your_server_ip

      The ssh command opens an SSH connection, but -L specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side (server). This means that whatever is running on the second port number (e.g. 8888) on the server will appear on the first port number (e.g. 8888) on your local computer.

      Optionally change port 8888 to one of your choosing to avoid using a port already in use by another process.

      server_username is your username (e.g. sammy) on the server which you created and your_server_ip is the IP address of your server.

      For example, for the username sammy and the server address 203.0.113.0, the command would be:

      • ssh -L 8888:localhost:8888 sammy@203.0.113.0

      If no error shows up after running the ssh -L command, you can move into your programming environment and run Jupyter Notebook:

      You’ll receive output with a URL. From a web browser on your local machine, open the Jupyter Notebook web interface with the URL that starts with http://localhost:8888. Ensure that the token number is included, or enter the token number string when prompted at http://localhost:8888.

      SSH Tunneling with Windows and Putty

      If you are using Windows, you can create an SSH tunnel using Putty.

      First, enter the server URL or IP address as the hostname as shown:

      Set Hostname for SSH Tunnel

      Next, click SSH on the bottom of the left pane to expand the menu, and then click Tunnels. Enter the local port number to use to access Jupyter on your local machine. Choose 8000 or greater to avoid ports used by other services, and set the destination as localhost:8888 where :8888 is the number of the port that Jupyter Notebook is running on.

      Now click the Add button, and the ports should appear in the Forwarded ports list:

      Forwarded ports list

      Finally, click the Open button to connect to the server via SSH and tunnel the desired ports. Navigate to http://localhost:8000 (or whatever port you chose) in a web browser to connect to Jupyter Notebook running on the server. Ensure that the token number is included, or enter the token number string when prompted at http://localhost:8000.

      Step 6 — Using Jupyter Notebook

      This section goes over the basics of using Jupyter Notebook. If you don’t currently have Jupyter Notebook running, start it with the jupyter notebook command.

      You should now be connected to it using a web browser. Jupyter Notebook is very powerful and has many features. This section will outline a few of the basic features to get you started using the notebook. Jupyter Notebook will show all of the files and folders in the directory it is run from, so when you’re working on a project make sure to start it from the project directory.

      To create a new notebook file, select New > Python 3 from the top right pull-down menu:

      Create a new Python 3 notebook

      This will open a notebook. We can now run Python code in the cell or change the cell to markdown. For example, change the first cell to accept Markdown by clicking Cell > Cell Type > Markdown from the top navigation bar. We can now write notes using Markdown and even include equations written in LaTeX by putting them between the $$ symbols. For example, type the following into the cell after changing it to markdown:

      # Simple Equation
      
      Let us now implement the following equation:
      $$ y = x^2$$
      
      where $x = 2$
      

      To turn the markdown into rich text, press CTRL+ENTER, and the following should be the results:

      results of markdown

      You can use the markdown cells to make notes and document your code. Let's implement that simple equation and print the result. Click on the top cell, then press ALT+ENTER to add a cell below it. Enter the following code in the new cell.

      x = 2
      y = x**2
      print(y)
      

      To run the code, press CTRL+ENTER. You’ll receive the following results:

      simple equation results

      You now have the ability to import modules and use the notebook as you would with any other Python development environment!

      Conclusion

      Congratulations! You should now be able to write reproducible Python code and notes in Markdown using Jupyter Notebook. To get a quick tour of Jupyter Notebook from within the interface, select Help > User Interface Tour from the top navigation menu to learn more.

      From here, you may be interested to read our series on Time Series Visualization and Forecasting.



      Source link