Python
- 1 Learning Python
- 2 pyenv
- 2.1.1 Documentation
- 2.1.2 Install pyenv
- 2.1.3 Verify pyenv is installed:
- 2.1.4 Show all available python version that can be installed
- 2.1.5 Install Python 3.7.16
- 2.1.6 Troubleshooting Install problems
- 2.1.7 Show current python
- 2.1.8 Switch to different version of python and show it
- 2.1.9 Update pip and setuptools
- 2.1.10 Installing packages
- 2.1.11 Listing packages
- 2.1.12 Installing pyenv in a shared directory
- 2.2 Considerations for installing pyenv on Centos 6
- 2.3 Remove pyenv
- 3 pipenv
Learning Python
http://learnpythonthehardway.org/book/
http://www.codecademy.com/en/tracks/python
Python User Group - Richmond, VA - https://www.meetup.com/PyRVAUserGroup/
Showing all available python modules
Three different ways of showing all available python modules
pip list
>>>help("modules")
>>>import sys
>>>print sys.modules
http://blog.revathskumar.com/2011/10/python-list-all-packages-installed.html
http://stackoverflow.com/questions/2550667/python-list-of-modules-helpmodules-not-working
pip
pip is a package management tool for python.
pip is installed by default in many linux distributions. To verify that pip is installed use the command:
which pip
If it is not installed, you need to manually install pip do the following:
http://www.pip-installer.org/en/latest/installing.html
Some python modules are installed by default by the operating system, but there are many more that can be installed. Not all modules are compatible with other modules, so they are not installed by default.
A user can use pip to install modules into a user's home directory without the need for administrator privileges:
pip install --user <module>
modules will be downloaded, compiled, and installed under ~/.local
To show all installed python modules use:
pip list
To search for python modules available for installation use:
pip search <module>
Before installing other modules, its recommend that pip and setuptools be updated:
pip install --user --upgrade pip
pip install --user --upgrade setuptools
Users on a cluster should qrsh to get a shell on a compute node before installing new packages.
pyenv
pyenv allows for multiple version of python to be installed in a users home directory and they are able to switch from one to another. If a user needs a version of python that is not installed by default, they can use pyenv to any almost any version they wish in their home directory. They can then use pip that comes with that version to install the modules they want. Read the documentation for pyenv before using these direction.
Documentation
https://github.com/pyenv/pyenv ← Please read this document
Install pyenv
( also installs easy_install and pip )
If you are on a cluster, use qrsh to obtain a shell on a compute node.
cdgit clone https://github.com/pyenv/pyenv.git .pyenv
Create a file to set the correct environmental variables for pyenv
Create a file called ~/enable_pyenv.sh with the following lines:
echo 'export PYENV_ROOT="$HOME/.pyenv"' > ./enable_pyenv.shecho 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ./enable_pyenv.shecho 'eval "$(pyenv init --path)"' >> ./enable_pyenv.shecho 'eval "$(pyenv init -)"' >> ./enable_pyenv.sh
At this point, if you are on a cluster, use qrsh to get a shell on a compute node.
activate these variables with the source command:
source ~/enable_pyenv.sh
If you want these to be set with every login, add "source ~/enable_pyenv.sh" to your ~/.bash_profile
Verify pyenv is installed:
which pyenv
pyenv --version
Show all available python version that can be installed
If you need a version of python3, its recommended to use the latest version of 3.6 or above since its likely to be used on future servers. But, not all python modules are available for every version of python, research supported version for a desired module.
pyenv install --list Output will be a huge list of python versions and should look something like the output below
There are many versions of python supported by pyenv. Most users want the latest version of 2.7.x or 3.7.x or above
Note: Python 2.7 will reach the end of its life on January 1st, 2020
Install Python 3.7.16
Basic Install:
pyenv install 3.7.16
Advanced install, creates log of install process:
pyenv install 3.7.16 -v 2>&1 | tee pyenv-install-3.7.16.log
The install takes several minutes to complete after the "Installing ..." message appears.
Troubleshooting Install problems
See https://github.com/pyenv/pyenv/wiki/common-build-problems
Show current python
which pythonpython -V$ pyenv versionssystem* 3.7.16 (set by .python-version)
Switch to different version of python and show it
python -Vwhich pythonpyenv local 3.7.16
python -Vwhich python
Note: when using pip from within the pyenv environment, there is no need to use the --user switch, because you have access to default python directories for version you have installed.
Update pip and setuptools
Before installing any new modules, its recommended to update pip and setuptools, which are used to install modules.
pip install --upgrade pip setuptools
or
python -m pip install --upgrade pip setuptools
Installing packages
pip install <package>
$ pip install numpy
Collecting numpy
Using cached numpy-1.19.5-cp36-cp36m-manylinux2010_x86_64.whl (14.8 MB)
Installing collected packages: numpy
Successfully installed numpy-1.19.5
Listing packages
This is list all installed packages
pip list
Installing pyenv in a shared directory
Installing in a shared directory is similar to installing in a user's home directory, but a change needs to be made to the source file to enable pyenv
In this example the shared directory is /vcu_gpfs2/home/sharedtest pyenv is installed at /vcu_gpfs2/home/sharedtest/.pyenv
cd /vcu_gpfs2/home/sharedtestgit clone git://github.com/yyuu/pyenv.git .pyenv# create source file, note the shared directory name is listed explicitly, change it for your directory echo 'export PYENV_ROOT="/vcu_gpfs2/home/sharedtest/.pyenv"' > ./enable_pyenv.shecho 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ./enable_pyenv.shecho 'eval "$(pyenv init --path)"' >> ./enable_pyenv.shecho 'eval "$(pyenv init -)"' >> ./enable_pyenv.sh# enable the pyenv in the shared directorysource /vcu_gpfs2/home/sharedtest/enable_pyenv.sh# verify you are using the pyenv in the shared directory:which pyenvoutput should look like this:/vcu_gpfs2/home/sharedtest/.pyenv/bin/pyenv
All other commands should work normally
Considerations for installing pyenv on Centos 6
Always get a qrsh shell to one of the cluster nodes
If a newer version of gcc is required, add the following line to enable_pyenv.sh:
source /opt/rh/devtoolset-7/enable
If a newer version of openssl is required, add the following line to enable_pyenv.sh:
source /usr/global/openssl/enable_openssl.sh
If you installed pyenv, add those lines to the begining of the enable_pyenv.sh file mentioned above.
Installing openssl 1.1.1
This is necessary for Centos 6 systems in order to install python 3.7.x . This has already been done for godel and is found at /usr/global/openssl
mkdir src/openssl-1.1.1scd src/openssl-1.1.1s
wget https://www.openssl.org/source/openssl-1.1.1s.tar.gz -O openssl-1.1.1s.tar.gz
tar -zxf openssl-1.1.1s.tar.gz
cd openssl-1.1.1s
./config --prefix=/usr/global/openssl-1.1.1s --openssldir=/usr/global/openssl-1.1.1s shared zlib 2>&1 | tee config-$(date +%F-%H%M).log
make 2>&1 | tee make-$(date +%F-%H%M).log
make test 2>&1 | tee make-test-$(date +%F-%H%M).log
make install 2>&1 | tee make-install-$(date +%F-%H%M).log
Error: Could not find a version that satisfies the requirement
When trying to install modules with pip, if the following error comes up:
$ pip install numpy Collecting numpy /home/cgchildr/.pyenv/versions/2.7.14/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning. SNIMissingWarning Could not find a version that satisfies the requirement numpy (from versions: ) No matching distribution found for numpy
You'll need to first install updated version of openssl using the following command:
$ pip install --trusted-host pypi.python.org pyOpenSSL ndg-httpsclient pyasn1 Collecting pyOpenSSL /home/cgchildr/.pyenv/versions/2.7.14/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning. SNIMissingWarning Downloading pyOpenSSL-17.5.0-py2.py3-none-any.whl (53kB) 100% |████████████████████████████████| 61kB 6.0MB/s Collecting ndg-httpsclient Downloading ndg_httpsclient-0.4.4-py2-none-any.whl Collecting pyasn1 Downloading pyasn1-0.4.2-py2.py3-none-any.whl (71kB) 100% |████████████████████████████████| 71kB 17.8MB/s Collecting six>=1.5.2 (from pyOpenSSL) Downloading six-1.11.0-py2.py3-none-any.whl Collecting cryptography>=2.1.4 (from pyOpenSSL) Downloading cryptography-2.2.2-cp27-cp27mu-manylinux1_x86_64.whl (2.2MB) 100% |████████████████████████████████| 2.2MB 18.8MB/s Collecting cffi>=1.7; platform_python_implementation != "PyPy" (from cryptography>=2.1.4->pyOpenSSL) Downloading cffi-1.11.5-cp27-cp27mu-manylinux1_x86_64.whl (407kB) 100% |████████████████████████████████| 409kB 16.3MB/s Collecting enum34; python_version < "3" (from cryptography>=2.1.4->pyOpenSSL) Downloading enum34-1.1.6-py2-none-any.whl Collecting asn1crypto>=0.21.0 (from cryptography>=2.1.4->pyOpenSSL) Downloading asn1crypto-0.24.0-py2.py3-none-any.whl (101kB) 100% |████████████████████████████████| 102kB 16.9MB/s Collecting idna>=2.1 (from cryptography>=2.1.4->pyOpenSSL) Downloading idna-2.6-py2.py3-none-any.whl (56kB) 100% |████████████████████████████████| 61kB 14.8MB/s Collecting ipaddress; python_version < "3" (from cryptography>=2.1.4->pyOpenSSL) Downloading ipaddress-1.0.19.tar.gz Collecting pycparser (from cffi>=1.7; platform_python_implementation != "PyPy"->cryptography>=2.1.4->pyOpenSSL) Downloading pycparser-2.18.tar.gz (245kB) 100% |████████████████████████████████| 256kB 18.0MB/s Installing collected packages: six, pycparser, cffi, enum34, asn1crypto, idna, ipaddress, cryptography, pyOpenSSL, pyasn1, ndg-httpsclient Running setup.py install for pycparser ... done Running setup.py install for ipaddress ... done Successfully installed asn1crypto-0.24.0 cffi-1.11.5 cryptography-2.2.2 enum34-1.1.6 idna-2.6 ipaddress-1.0.19 ndg-httpsclient-0.4.4 pyOpenSSL-17.5.0 pyasn1-0.4.2 pycparser-2.18 six-1.11.0
After that, python modules with install normally:
$ pip install numpy Collecting numpy /home/cgchildr/.pyenv/versions/2.7.14/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning. SNIMissingWarning Downloading numpy-1.14.2-cp27-cp27mu-manylinux1_x86_64.whl (12.1MB) 100% |████████████████████████████████| 12.1MB 66kB/s Installing collected packages: numpy Successfully installed numpy-1.14.2
Example: Install Python 3.8.16 under Centos 6 on godel
On godel first do: qrsh
On servers with Centos 6 ( like godel ) an updated version of openssl must be used with python 3.7.x, do the following to install python 3.7.x with pyenv:
source ~/enable_pyenv.shsource /opt/rh/devtoolset-7/enablesource /usr/global/openssl/enable_openssl.sh# verify gcc and openssl are updatedwhich gccwhich opensslexport LDFLAGS="-Wl,-rpath,/usr/global/openssl/lib" export CPPFLAGS="-I/usr/global/openssl/include" export CONFIGURE_OPTS="--with-openssl=/usr/global/openssl" pyenv install 3.8.13 -v 2>&1 | tee pyenv-install-3.8.13.log
The install takes several minutes to complete after the "Installing ..." message appears.
Remove pyenv
remove ( or move ) the following
rm -rf ~/.pyenv
rm ~/.python-version
rm ~/enable_pyenv.sh
pipenv
research