Python Azure Function Debugging in Pyenv-virtualenv virtual environment

Creating virtual environment

$ mkdir python_demo
$ cd python_demo
$ pyenv local 3.9.16
$ pyenv virtualenv 3.9.16 python_demo-3.9.16

# OR pyenv-virtualenv will use local python runtime with the following command
$ pyenv virtualenv python_demo-3.9.16

setting pyenv local version to created virtual environment will allow to activate virtual environment automatically when we enter project folder, so exiting from project folder will deactivate virtual environment.

$ pyenv local python_demo-3.9.16

Activating virtual environment

$ pyenv activate python_demo-3.9.16
Continue reading

Python Azure Function Debugging in Conda virtual environment

Creating virtual environment

$ mkdir python_demo
$ cd python_demo

don't forget to update name of environment name, and i added pip, ptvsd packages to debug azure function.

$ cat > environment.yml <<EOL
name: python_demo-3.9
channels:
  - defaults
dependencies:
  - python=3.9
  - pip
  - pip:
    - ptvsd
EOL
$ conda env create -f environment.yml

Collecting package metadata (repodata.json): done
Solving environment: done

Downloading and Extracting Packages

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Installing pip dependencies: - Ran pip subprocess with arguments:
['/opt/homebrew/Caskroom/miniconda/base/envs/python_azure_function_conda_virtual_env-3.9/bin/python', '-m', 'pip', 'install', '-U', '-r', '/Users/kenanhancer/Documents/projects/python-projects-kenanhancer/python_azure_function_conda_virtual_env/condaenv.5uk0y5ie.requirements.txt', '--exists-action=b']
Pip subprocess output:
Collecting ptvsd (from -r /Users/kenanhancer/Documents/projects/python-projects-kenanhancer/python_azure_function_conda_virtual_env/condaenv.5uk0y5ie.requirements.txt (line 1))
  Using cached ptvsd-4.3.2-py2.py3-none-any.whl (4.9 MB)
Installing collected packages: ptvsd
Successfully installed ptvsd-4.3.2

done
#
# To activate this environment, use
#
#     $ conda activate python_azure_function_conda_virtual_env-3.9
#
# To deactivate an active environment, use
#
#     $ conda deactivate
Continue reading

conda vs pipenv vs virtualenv commands

find more information in conda

TaskConda package and environment manager commandPip package manager commandVirtualenv environment manager command
Install a packageconda install $PACKAGE_NAMEpip install $PACKAGE_NAMEX
Update a packageconda update --name $ENVIRONMENT_NAME$PACKAGE_NAMEpip install --upgrade$PACKAGE_NAMEX
Update package managerconda update condaLinux/macOS: pip install -Upip Win: python -m pipinstall -U pipX
Uninstall a packageconda remove --name $ENVIRONMENT_NAME$PACKAGE_NAMEpip uninstall $PACKAGE_NAMEX
Create an environmentconda create --name $ENVIRONMENT_NAME pythonXcd $ENV_BASE_DIR; virtualenv$ENVIRONMENT_NAME
Activate an environmentconda activate $ENVIRONMENT_NAME*Xsource$ENV_BASE_DIR/$ENVIRONMENT_NAME/bin/activate
Deactivate an environmentconda deactivateXdeactivate
Search available packagesconda search $SEARCH_TERMpip search $SEARCH_TERMX
Install package from specific sourceconda install --channel $URL $PACKAGE_NAMEpip install --index-url $URL$PACKAGE_NAMEX
List installed packagesconda list --name $ENVIRONMENT_NAMEpip listX
Create requirements fileconda list --exportpip freezeX
List all environmentsconda info --envsXInstall virtualenv wrapper, then lsvirtualenv
Install other package managerconda install pippip install condaX
Install Pythonconda install python=x.xXX
Update Pythonconda update python*XX

How to create virtual environment with conda

Conda is an open-source package management system and virtual environment management system that runs on Windows, macOS, and Linux. It was created for Python programs but it can package and distribute software for any language such as R, Ruby, Lua, Scala, Java, JavaScript, C, C++, FORTRAN.

The two main purposes of Conda are:

  1. Package management: Conda makes it easy to manage and install packages, even for different versions of Python. In addition, it also supports binary package management, which makes it an efficient way to handle packages and dependencies in your projects.
  2. Virtual Environment management: Conda allows you to create separate environments containing files, packages, and their dependencies that will not interact with other environments. When switching between Python versions for different projects, Conda makes it simple to use the specific version you need.

While pip can install Python packages, Conda can install packages for any language. Conda packages are binaries, which eliminates the need to compile the code which makes installations faster and more straightforward.

Installing conda

$ brew update
$ brew install --cask miniconda

Checking conda version

$ conda --version

conda 23.3.1

Conda help

$ conda -h
usage: conda [-h] [-V] command ...

conda is a tool for managing and deploying applications, environments and packages.

Options:

positional arguments:
  command
    clean             Remove unused packages and caches.
    compare           Compare packages between conda environments.
    config            Modify configuration values in .condarc. This is modeled after the git config command. Writes to the
                      user .condarc file (/Users/kenanhancer/.condarc) by default. Use the --show-sources flag to display
                      all identified configuration locations on your computer.
    create            Create a new conda environment from a list of specified packages.
    info              Display information about current conda install.
    init              Initialize conda for shell interaction.
    install           Installs a list of packages into a specified conda environment.
    list              List installed packages in a conda environment.
    package           Low-level conda package utility. (EXPERIMENTAL)
    remove (uninstall)
                      Remove a list of packages from a specified conda environment. Use `--all` flag to remove all packages
                      and the environment itself.
    rename            Renames an existing environment.
    run               Run an executable in a conda environment.
    search            Search for packages and display associated information.The input is a MatchSpec, a query language for
                      conda packages. See examples below.
    update (upgrade)  Updates conda packages to the latest compatible version.
    notices           Retrieves latest channel notifications.

options:
  -h, --help          Show this help message and exit.
  -V, --version       Show the conda version number and exit.

conda commands available from other packages (legacy):
  content-trust
  env
Continue reading

How to create virtual environment with pipenv

Pipenv is a Python tool that aims to bring the best features of multiple other tools into one. It provides an easy way to manage virtual environments and manage package dependencies consistently. Its key goal is to simplify the workflow of managing a Python environment for your project.

Installing pipenv

$ python -m pip install pipenv

Checking pipenv version

$ pipenv --version

pipenv, version 2022.7.4

Checking pipenv version

$ pipenv -h
Usage: pipenv [OPTIONS] COMMAND [ARGS]...

Options:
  --where                         Output project home information.
  --venv                          Output virtualenv information.
  --py                            Output Python interpreter information.
  --envs                          Output Environment Variable options.
  --rm                            Remove the virtualenv.
  --bare                          Minimal output.
  --man                           Display manpage.
  --support                       Output diagnostic information for use in
                                  GitHub issues.
  --site-packages / --no-site-packages
                                  Enable site-packages for the virtualenv.
                                  [env var: PIPENV_SITE_PACKAGES]
  --python TEXT                   Specify which version of Python virtualenv
                                  should use.
  --three                         Use Python 3 when creating virtualenv.
  --clear                         Clears caches (pipenv, pip).  [env var:
                                  PIPENV_CLEAR]
  -q, --quiet                     Quiet mode.
  -v, --verbose                   Verbose mode.
  --pypi-mirror TEXT              Specify a PyPI mirror.
  --version                       Show the version and exit.
  -h, --help                      Show this message and exit.


Usage Examples:
   Create a new project using Python 3.7, specifically:
   $ pipenv --python 3.7

   Remove project virtualenv (inferred from current directory):
   $ pipenv --rm

   Install all dependencies for a project (including dev):
   $ pipenv install --dev

   Create a lockfile containing pre-releases:
   $ pipenv lock --pre

   Show a graph of your installed dependencies:
   $ pipenv graph

   Check your installed dependencies for security vulnerabilities:
   $ pipenv check

   Install a local setup.py into your virtual environment/Pipfile:
   $ pipenv install -e .

   Use a lower-level pip command:
   $ pipenv run pip freeze

Commands:
  check         Checks for PyUp Safety security vulnerabilities and against
                PEP 508 markers provided in Pipfile.
  clean         Uninstalls all packages not specified in Pipfile.lock.
  graph         Displays currently-installed dependency graph information.
  install       Installs provided packages and adds them to Pipfile, or (if no
                packages are given), installs all packages from Pipfile.
  lock          Generates Pipfile.lock.
  open          View a given module in your editor.
  requirements  Generate a requirements.txt from Pipfile.lock.
  run           Spawns a command installed into the virtualenv.
  scripts       Lists scripts in current environment config.
  shell         Spawns a shell within the virtualenv.
  sync          Installs all packages specified in Pipfile.lock.
  uninstall     Uninstalls a provided package and removes it from Pipfile.
  update        Runs lock, then sync.
  verify        Verify the hash in Pipfile.lock is up-to-date.
Continue reading

How to create virtual environment with pyenv-virtualenv plugin

pyenv-virtualenv is a pyenv plugin that provides features to manage virtualenvs and conda environments for Python on UNIX-like systems.

If you don't have pyenv in your system, follow below post;

Installing pyenv-virtualenv

$ brew update
$ brew install pyenv-virtualenv

Setting PATH

Run one of the following commands in terms of your shell's .rc file.

$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrc

Creating virtual environment

$ mkdir python_demo
$ cd python_demo
$ pyenv install 3.11.4
$ pyenv virtualenv 3.11.4 python_demo-3.11.4
$ pyenv virtualenvs
$ pyenv prefix python_demo-3.11.4
/Users/kenanhancer/.pyenv/versions/python_demo-3.11.4
$ ls -Llat $(pyenv prefix python_demo-3.11.4)

drwxr-xr-x  14 kenanhancer  staff  448 19 Jun 17:36 bin
-rw-r--r--   1 kenanhancer  staff  107 19 Jun 17:36 pyvenv.cfg
drwxr-xr-x   3 kenanhancer  staff   96 19 Jun 17:36 lib
drwxr-xr-x   2 kenanhancer  staff   64 19 Jun 17:36 include
$ tree -a -L 4 $(pyenv prefix python_demo-3.11.4)

├── bin
│   ├── Activate.ps1
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── pip
│   ├── pip3
│   ├── pip3.11
│   ├── pydoc
│   ├── python -> python3.11
│   ├── python3 -> python3.11
│   └── python3.11 -> /Users/kenanhancer/.pyenv/versions/3.11.4/bin/python3.11
├── include
│   └── python3.11
├── lib
│   └── python3.11
│       └── site-packages
│           ├── _distutils_hack
│           ├── distutils-precedence.pth
│           ├── pip
│           ├── pip-23.1.2.dist-info
│           ├── pkg_resources
│           ├── setuptools
│           └── setuptools-65.5.0.dist-info
└── pyvenv.cfg
Continue reading

How to install Python packages with pipenv, venv or virtualenv package manager

You can find GitHub repository in https://github.com/python-projects-kenanhancer/python_demo

  • Pyenv – Python Runtime Version Manager
    – to install, uninstall, list or switch between different python runtimes
  • Pipenv – Python Package Manager and Virtual environment manager
    – to create an isolated virtual environment with a specific python runtime
    – to install, uninstall, list python packages
  • Venv – Virtual environment manager (for Python 3)
    – to create an isolated virtual environment with a specific python runtime
    – python packages will be isolated already
  • Virtualenv – Virtual environment manager (for Python 2)
    – to create an isolated virtual environment with a specific python runtime
    – python packages will be isolated already

Installing Python Runtime Versions via Pyenv

In order to test different Python Runtime versions, I have installed some versions.

$ pyenv install 3.10.2
$ pyenv install 3.10.3
$ pyenv install 3.10.4
$ pyenv install 3.10.5

I have 4 Python runtime versions(3.10.2, 3.10.3, 3.10.4, 3.10.5) in my machine. So, let's list them.

$ pyenv versions

  system
  3.10.2
  3.10.3
  3.10.4
* 3.10.5 (set by /Users/kenanhancer/.pyenv/version)
Continue reading

How to install Java with Jabba

Runtime version managers make developers life easier. So, I tested different Java version managers. But, Jabba is the easiest one for me.

You can read previously posted How to install java articles.

Java Specification Versions:

Here are the major releases:

  1. Java 1.0 (January 1996) – The original version.
  2. Java 1.1 (February 1997) – Introduced several significant features, including the JavaBeans API.
  3. Java 2 (J2SE 1.2) (December 1998) – Introduced the Swing application framework.
  4. J2SE 1.3 (May 2000) – Introduced the HotSpot JVM.
  5. J2SE 1.4 (February 2002) – Introduced regular expressions, image I/O API, and more.
  6. Java SE 5 (J2SE 1.5) (September 2004) – Introduced generics, metadata annotations, enumerated types, and more.
  7. Java SE 6 (December 2006) – Introduced scripting support, JVM improvements, and more.
  8. Java SE 7 (July 2011) – Introduced the try-with-resources statement, the diamond operator, and more.
  9. Java SE 8 (March 2014) – Introduced lambdas, streams, and the java.time package.
  10. Java SE 9 (September 2017) – Introduced the module system.
  11. Java SE 10 (March 2018) – Introduced local-variable type inference (var).
  12. Java SE 11 (September 2018) – Introduced String::lines, isBlank, and other methods, plus the java.net.http HttpClient.
  13. Java SE 12 (March 2019) – Introduced switch expressions (preview) and more.
  14. Java SE 13 (September 2019) – Introduced text blocks (preview) and more enhancements.
  15. Java SE 14 (March 2020) – Introduced pattern matching for instanceof (preview) and other improvements.
  16. Java SE 15 (September 2020) – Continued the introduction and enhancement of several features.
  17. Java SE 16 (March 2021) – Introduced sealed classes (preview) and other enhancements.
  18. Java SE 17 (September 2021) – This version is an LTS (Long Term Support) release, which means it will receive updates for a longer period than the interim releases.
  19. Java SE 18 (March 2022)
  20. Java SE 19 (September 2022)
  21. Java SE 20 (March 2023)
  22. Java SE 21 (September 2023)

Java has moved to a time-driven release model, with new versions being released every six months. Not all of these are long-term support (LTS) versions. As of Java 17, the LTS versions have been Java 8, Java 11, and Java 17.

Installing Jabba

Click Jabba link to see more detailed information about Jabba

$ curl -sL https://github.com/shyiko/jabba/raw/master/install.sh | bash && . ~/.jabba/jabba.sh

Usage

# list available JDK's
$ jabba ls-remote

# you can use any valid semver range to narrow down the list
$ jabba ls-remote zulu@~1.8.60
$ jabba ls-remote "*@>=1.6.45 <1.9" --latest=minor

# install Oracle JDK
$ jabba install 1.8

# install Oracle Server JRE
$ jabba install sjre@1.8

# install Adopt OpenJDK (Hotspot)
$ jabba install adopt@1.8-0

# install Adopt OpenJDK (Eclipse OpenJ9)
$ jabba install adopt-openj9@1.9-0

# install Zulu OpenJDK
$ jabba install zulu@1.8
$ jabba install zulu@~1.8.144 # same as "zulu@>=1.8.144 <1.9"

# install IBM SDK, Java Technology Edition
$ jabba install ibm@1.8

# install GraalVM CE
$ jabba install graalvm@1.0-0

# install OpenJDK
$ jabba install openjdk@1.10-0

# install OpenJDK with Shenandoah GC
$ jabba install openjdk-shenandoah@1.10-0

# install from custom URL
# (supported qualifiers: zip (since 0.3.0), tgz, tgx (since 0.10.0), dmg, bin, exe)
$ jabba install 1.8.0-custom=tgz+http://example.com/distribution.tar.gz
$ jabba install 1.8.0-custom=tgx+http://example.com/distribution.tar.xz
$ jabba install 1.8.0-custom=zip+file:///opt/distribution.zip

# uninstall JDK
$ jabba uninstall zulu@1.6.77

# link system JDK
$ jabba link system@1.8.72 /Library/Java/JavaVirtualMachines/jdk1.8.0_72.jdk

# list all installed JDK's
$ jabba ls

# switch to a different version of JDK (it must be already `install`ed)
$ jabba use adopt@1.8
$ jabba use zulu@~1.6.97

$ echo "1.8" > .jabbarc
# switch to the JDK specified in .jabbarc (since 0.5.0)
$ jabba use

# set default java version on shell (since 0.2.0)
# this version will automatically be "jabba use"d every time you open up a new terminal
$ jabba alias default 1.8
Continue reading

How to install Java with brew and jenv

jenv is a tool that helps you switch between different java versions in your development machine. But, Jenv doesn't install Java for you.

Use your platform appropriate package manager to install Java. On macOS, brew is recommended.

Install Java with Homebrew

First, ensure that you have Homebrew installed in your system. Homebrew is a package manager for Mac OS. So if you want to learn more details about it, visit my blog below.

I also posted how to install Java JDK manually in the following post.

Actually I already mentioned how to install Java JDK in the above post, but I will shortly show one more time

$ brew tap adoptopenjdk/openjdk
$ brew search jdk

==> Formulae
openjdk                                                                              openjdk@11

==> Casks
adoptopenjdk
adoptopenjdk12
adoptopenjdk13-openj9
adoptopenjdk14-openj9-jre-large
adoptopenjdk9
adoptopenjdk10
adoptopenjdk12-jre
adoptopenjdk13-openj9-jre
adoptopenjdk14-openj9-large
jdk-mission-control
adoptopenjdk11
adoptopenjdk12-openj9
adoptopenjdk13-openj9-jre-large
adoptopenjdk8
oracle-jdk
adoptopenjdk11-jre
adoptopenjdk12-openj9-jre
adoptopenjdk13-openj9-large
adoptopenjdk8-jre
oracle-jdk-javadoc
adoptopenjdk11-openj9
adoptopenjdk12-openj9-jre-large
adoptopenjdk14
adoptopenjdk8-openj9
sapmachine-jdk
adoptopenjdk11-openj9-jre
adoptopenjdk12-openj9-large
adoptopenjdk14-jre
adoptopenjdk8-openj9-jre
adoptopenjdk11-openj9-jre-large
adoptopenjdk13
adoptopenjdk14-openj9
adoptopenjdk8-openj9-jre-large
adoptopenjdk11-openj9-large
adoptopenjdk13-jre
adoptopenjdk14-openj9-jre
adoptopenjdk8-openj9-large
$ brew cask install adoptopenjdk
$ java --version

openjdk 14.0.1 2020-04-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.1+7)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.1+7, mixed mode, sharing)
Continue reading