How to specify python version to use when leveraging pip - python-3.x

I'm trying to run pip to install some libraries for python 2.7, but in my windows shell, its identifying it with python 3.X that I have installed and installing the packages to the python libs folder, or some folder relative to python 3.x. How can I specify pip to install specified package to only 2.7 in the CLI instead of python 3, and vice-versa?
Perhaps I should try to use virtual environment with python 3 running and run pip from there?
pip install pyvisa
I expect this to be an issue because I have no unique environment variable to specify which version of python I want pip to use.

Try py -2 -m pip install pyvisa for Python 2 and py -3 -m pip install pyvisa for Python 3. The py launcher that allows you to run stuff both in Python 2 and in Python 3 comes bundled with recent versions of both Python 2 and Python 3 for Windows, but it's optional. If you chose not to install py, you can instead use C:\Python2\python.exe -m pip install pyvisa, as an example -- substitute your own Python 2 path there.

Related

ModuleNotFoundError: No module named 'harfang'

PLEASE ,how to import harfang in ubunto for python3 ,?? it doesn't install for me !!
i tryed pip install harfang and doesn't work
Solution
The solution is to simply use
pip3 install harfang
or
python3 -m pip install harfang
If it throws error regarding pip not being installed, you can install python3-pip using apt.
Explanation
Why you were unable to install with pip directly and instead had to use pip3 is because, in ubuntu, the default python version is python 2.x. The current python versions actively maintained and supported is 3.x where x means some minor version.
Due to this reason, after you install a python 3.x installation, you need to specify if it's python3 or python2, where python is a default alias for python2.
I also mentioned about python3 -m pip install harfang. This also works, as python3 -m pip means trying to run a python module using specific python installation (which in our case is python3), you can specify which module to call using -m. You can do it with any other installed python module which has a CLI interface as well.

How to create python 2.7 virtual environment using python 3.7

I have Python 3.7 && I would like to create a python 2.7 virtual environment to run some code that only works on python 2.7
How do I create this python 2.7 virtual environment?
python3 -m venv ?
When creating virtual environment, a pyvenv.cfg is created that has home key which tells where the python executable is, which was used to create the virtual environment. If your global python installation is version 3.8.6, and you run
python3 -m venv something
you will create virtual environment in folder something, that has pyvenv.cfg that points to the python executable of the Python 3.8.6 installation. There is no easy way* to make it point to the Python 2.7 executable.
What can you do?
virtualenv as venv replacement
The venv module was introduced in Python 3.3, so you cannot use it to create virtual environments with python 2.7. You could use the virtualenv package which is a superset of venv. First, install it with python 2.7**:
python -m pip install virtualenv
If Python 2.7 is not on your PATH as python, use full path to the python executable in place of python. Then, you can create virtual environments that have Python 2.7 with
virtualenv something
or
virtualenv --python=python2.7 something
* It is not supported by the venv module out of the box, at least.
** You can actually install it with any Python version, but then you will have to specify --python=/opt/python-2.7/bin/python or --python=python2.7 when running virtualenv. By default, it uses the python executable that was used to install it.
venv doesn’t allow creating virtual environments with other versions of Python than the one currently installed. You would have to use the traditional virtualenv package which allows creating virtual environments for different versions of python by providing the path to the binary like this:
virtualenv --python=/usr/bin/python2.7 /path/to/virtualenv/
where the path /usr/bin/python2.7 refers to the path of the binary for Python 2.7 on your system.
Install python2.7
Add universe repo
sudo apt-add-repository universe
sudo apt update
Install python2.7
sudo apt install python2-minimal
Create virtualenv with python2.7
mkvirtualenv -p $(which python2) something

My pip isn't working, for both versions of python

so i have installed both python 2.7 and python 3.8 however in the cmd if i type one of these:
pip install
pip --version
it uses the python 2.7 version. I don't think that i can use the packages installed with python 3.
i have tried:
Reinstalling python (2 & 3)
Adding it to my PATH
Changing the directory that it is saved in
Pip3 commands
bootstraping pip
yet none have worked.(also im on windows 7, and require the pip and python for both 2.7 & 3.8)
when i use:
python3 -m pip install x
or
py -3 -m pip install x
it comes up with:
DLL load failed while importing pyexpat: the specified module could
not be found
Python 3 command in Windows is python3. So if you want to run pip with python 3 you have to do python3 -m pip install.
You can also replace Python 2 path in your Path variable to your Python 3 path, like so when you'll run python command it will use Python 3 directly.
Hope i got useful.
Have a look at the official document,
https://docs.python.org/3/installing/index.html?highlight=pip#work-with-multiple-versions-of-python-installed-in-parallel
if you are on windows you can use,
py -2 -m pip install SomePackage # default Python 2
py -2.7 -m pip install SomePackage # specifically Python 2.7
py -3 -m pip install SomePackage # default Python 3
py -3.4 -m pip install SomePackage # specifically Python 3.4
On Linux, Mac OS X, and other POSIX systems, use the versioned Python commands in combination with the -m switch to run the appropriate copy of pip:
python2 -m pip install SomePackage # default Python 2
python2.7 -m pip install SomePackage # specifically Python 2.7
python3 -m pip install SomePackage # default Python 3
python3.4 -m pip install SomePackage # specifically Python 3.4
hope this helps!
If you install both versions of python then by simply entering pip command will run that python pip whose path comes first. You could use pip3 for pip corresponding to python3 and pip2 for pip corresponding to python2. But the recommended way is to do following:
For Python 3
py -3 -m pip install
For Python 2
py -2 -m pip install

Trying to install Python3 using brew

Trying to install Python3 in mac using below command :
brew install python3
When i run the command getting below error :
Error: python 2.7.14_2 is already installed
To upgrade to 3.6.5, run `brew upgrade python`
How to keep both python2 and python3 in mac without upgrading...
Thanks!
The python formula is assumed by Homebrew to be Python 3. The formula python3 is thus an alias for python.
You need to:
brew upgrade python, as told by the error message. It will switch your default Homebrew Python from 2 to 3.
brew install python#2. It will install Python 2 alongside Python 3.
Note however that even with Python 3 installed (using the formula called python), the command python still points to Python 2. You need to either type python3 to run Python 3, or add Homebrew’s Python 3 unprefixed bin directory at the beginning of your $PATH:
export PATH="$(brew --prefix python)/libexec/bin:$PATH"

How to install modules in Python 2.7 insted of Python 3.6?

I have two versions of Python in my laptop. Python 2.7 and Python 3.6. If install a module this is installed only in Python 3.6.
I would like to install modules in Python 2.7 through pip but I don't know how to do it.
I want to install right now GDAL and Fiona for Python 2.7 in Ubuntu 17.04.
If Python 2.7 is well installed on your system, you should have python2 and/or python2.7 commands and you could run the following:
python2.7 -m pip install <your-packages>
To make sure you are running the correct python version, you can use python2.7 --version
Better use virtual environment for this.
Follow this link https://realpython.com/blog/python/python-virtual-environments-a-primer/
You can set python version to use in virtual env using
virtualenv -p path/to/python2.7 env_name
Activate this env using . env_name/bin/activate then,
Use pip install package_name to install libraries inside virtual environment

Resources