One place for hosting & domains

      Python

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


      Introduction

      Python is a flexible and versatile programming language suitable 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 with immediate feedback on errors, Python is a useful language to learn 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 10 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 10 server. To learn how to achieve this setup, follow our Debian 10 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.7.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 10 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 10


      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, Anaconda is commonly used for large-scale data processing, scientific computing, and predictive analytics.

      Offering a collection of over 1,000 packages to support users working with data, Anaconda is available in both free and paid enterprise versions. 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 10 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 10 initial server setup guide.

      Installing Anaconda

      To install Anaconda on a Debian 10 server, you should download the latest Anaconda installer bash script, verify it, and then run it.

      Find the latest version of Anaconda for Python 3 at the Anaconda Distribution page. At the time of writing, the latest version is 2019.03, 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:

      sudo apt install curl
      

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

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

      At this point, we can 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-2019.03-Linux-x86_64.sh

      You’ll receive output that looks similar to this:

      Output

      45c851b7497cc14d5ca060064394569f724b67d9b5f98a926ed49b834a6bb73a Anaconda3-2019.03-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-2019.03-Linux-x86_64.sh

      You’ll receive the following output:

      Output

      Welcome to Anaconda3 2019.03 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 initialize Anaconda3 by running conda init? [yes|no] [no] >>>

      Type yes so that you do not need to add Anaconda to the PATH manually.

      Output

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

      You can now activate the installation by sourcing the ~/.bashrc file:

      • source ~/anaconda3/bin/activate

      You will now be in Anaconda’s base programming environment that is automatically named base. Your prompt will change to reflect this.

      Now, you can run the conda init command to initialize your environment.

      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 py37_0 alabaster 0.7.12 py37_0 anaconda 2019.03 py37_0 ...

      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.3

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

      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 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, there are a few steps to take to ensure it is entirely off of your system.

      First, deactivate the base Anaconda environment you’re in.

      Next, install 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:

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

      Output

      Backup directory: /home/sammy/.anaconda_backup/2019-07-09T020356

      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 script that initializes conda.

      /home/sammy/.bashrc

      ...
      # >>> conda initialize >>>
      # !! Contents within this block are managed by 'conda init' !!
      # __conda_setup="$('/home/sammy/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
      # if [ $? -eq 0 ]; then
      #     eval "$__conda_setup"
      # else
      #     if [ -f "/home/sammy/anaconda3/etc/profile.d/conda.sh" ]; then
      #         . "/home/sammy/anaconda3/etc/profile.d/conda.sh"
      #     else
      #         export PATH="/home/sammy/anaconda3/bin:$PATH"
      #     fi
      # fi
      # unset __conda_setup
      # <<< conda initialize <<<
      

      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 brought 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

      Python Machine Learning Projects — A DigitalOcean eBook


      Download the Complete eBook!

      Machine Learning Projects: Python eBook in EPUB format

      Machine Learning Projects: Python eBook in PDF format

      Machine Learning Projects: Python eBook in Mobi format

      Introduction to the eBook

      As machine learning is increasingly leveraged to find patterns, conduct analysis, and make decisions — sometimes without final input from humans who may be impacted by these findings — it is crucial to invest in bringing more stakeholders into the fold. This book of Python projects in machine learning tries to do just that: to equip the developers of today and tomorrow with tools they can use to better understand, evaluate, and shape machine learning to help ensure that it is serving us all.

      This book will set you up with a Python programming environment if you don’t have one already, then provide you with a conceptual understanding of machine learning in the chapter “An Introduction to Machine Learning.” What follows next are three Python machine learning projects. They will help you create a machine learning classifier, build a neural network to recognize handwritten digits, and give you a background in deep reinforcement learning through building a bot for Atari.

      These chapters originally appeared as articles on DigitalOcean Community, written by members of the international software developer community. If you are interested in contributing to this knowledge base, consider proposing a tutorial to the Write for DOnations program. DigitalOcean offers payment to authors and provides a matching donation to tech-focused nonprofits.

      Other Books in this Series

      If you are learning Python or are looking for reference material, you can download our free Python eBook, How To Code in Python 3

      For other programming languages and DevOps engineering articles, check out our knowledge base of over 2,100 tutorials.

      Download the eBook

      You can download the eBook in either the EPUB, PDF, or Mobi format by following the links below.

      Download the Complete eBook!

      Machine Learning Projects: Python eBook in EPUB format

      Machine Learning Projects: Python eBook in PDF format

      Machine Learning Projects: Python eBook in Mobi format



      Source link