skip to Main Content

I have a CentOs system with both Python 2.7 and Python 3.4 installed. I do not have information regarding how these were installed.

However, this is the response when the following commands are issued:

whereis /usr/bin/python3

python3: 
/usr/bin/python3.4 
/usr/bin/python3.4m 
/usr/lib/python3.4 
/usr/lib64/python3.4 
/usr/include/python3.4m
whereis /usr/bin/python2.7

python2: 
/usr/bin/python2.7 
/usr/bin/python2 
/usr/lib/python2.7 
/usr/lib64/python2.7 
/usr/include/python2.7 
/usr/share/man/man1/python2.1.gz

I am uncertain how to use PIP in this setup. Python documentation for PIP mentions it assumes that your environment is virtual.

If I want to install a module in python3.4 using PIP, what are the steps?
1. sudo as root?
2. set environmental variables?
3. etc…

2

Answers


  1. Installing a Python package is fairly straightforward, you need to first verify that it’s not already packaged by your distro, in your case:

    yum search <module_name>
    

    Those packages are generally named like: python-<module_name> for a Python 2 module and python3-<module_name> for a Python 3 module.

    If it doesn’t exist as a package, you can then rely on PyPI:

    python3 -m pip install <module_name> --user
    

    From my answer here
    Let’s break the this command in two parts:

    • python -m: Allows modules to be located using the Python module namespace for execution as scripts. The motivating examples were standard library modules such as pdb and profile. See PEP 338

    • --user : By default Python installs Python packages to system directories which requires root privileges, to avoid using sudo pip install (which is not recommended by the way) use this flag to make pip install packages in your home directory instead, which doesn’t require any special privileges.

    As a side note, if you have multiple versions of Python installed, keeping track of which Python version version pip is bound to can be a PITA, hence python -m, in this case you’re sure that it’s the pip bound to the Python called which will be executed.

    While the previous method works (kinda), it’s advised to use virtual environments because many Linux distributions (including CentOs) rely on some Python modules and you don’t want to modify them unless you know what you are doing or you absolutely want to break your System.

    Additionally, if you only want to “Install and Run Python Applications in Isolated Environments”, you can check out pipx.

    Login or Signup to reply.
  2. I always use pip3 when I want to directly refer to python3.

    If I want to install a module in python3.4 using PIP, what are the steps? 1. sudo as root? 2. set environmental variables? 3. etc…

    just this

    sudo apt install python3-pip

    pip3 install xyz

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search