install python package to a specific version of python? - python

I have a remote machine with python 2.6 as the default package on the machine. I installed numpy using yum install numpy.
I then installed python 2.7 using the instructions available python 2.7 install link in the directory /usr/src/. I then put this alias
alias python=/usr/src/Python-2.7.8/python
in ~/.bashrc.
So numpy is already installed on the machine but python 2.7 can't pick it up? Should I uninstall it? How do I install it for version 2.7.
The commands would be vary helpful.
Could I pip install to a specific version? I tried this command but it is incorrect.
pip-2.7 install numpy
Thanks

you can do
pythonx.y -m pip install numpy
to install numpy on python version x.y

Related

Why can I use NumPy with Python 3.8 which was preinstalled but not with Python 3.9 which I installed in addition?

I installed Ubuntu 20.04, and python3 (3.8) is installed by default.
To install python3.9, I executed:
sudo apt install python3.9
Thus, I have two versions of Python at my laptop: 3.8 and 3.9.
I'm trying to launch the simple script:
import numpy
With Python 3.8 it works correctly. But, when I interp my script by Python 3.9 the error occurs:
How to solve this problem?
I've already tried to update numpy using pip, nothing has happened.
pip3 install numpy --upgrade
Likely what is happening here is, because you have 2 versions of python installed, your pip3 command is only installing things for one of the versions, 3.8 in this case.
Specifying the python version you want to run your command with will help solve this issue.
For example,
python3.9 -m pip install numpy
or
python3.8 -m pip install numpy
You can install a package for a specific version of Python with
python3.9 -m pip install numpy
For a more consistent python environment look at python venv or a container.

How can I ensure my Python packages are installed in Python 3.10 instead of /Library/Python 3.8?

I have successfully installed Python 3.10. I want to install packages. Pandas, BeautifulSoup4, and requests.
I tried to import NumPy but IDE says it doesn't have the NumPy module.
I searched for it in directories and it turned out NumPy is in the 3.8 version.
I used the command on the terminal:
sudo python3 -m pip install Pandas
Which installed it successfully.
But the package files are installed in Python 3.8.
Now I have the following Directories:
/Library/Python 3.8
/Library/Frameworks/Python.framework/Versions 3.10
/Applications/Python 3.10
/usr/local/bin 3.10
In /Library/Python there is only 2.7 and 3.8 versions
In following directories version is 3.10:
/Library/Frameworks/Python.framework/Versions 3.10
/Applications/Python 3.10
/usr/local/bin 3.10
/usr/local/bin
/Library/Frameworks/Python.framework/Versions
/Applications/Python
What will happen if i remove 3.8 or can i update it to 3.10?
Is better to use a virtual environment and install in it python 3.10, removing Python 3.8 can may some problems, so avoid removing it, and if you are open to using different tools, I advise you to use Jupyter Notebook.
Don't follow this (go to EDIT instead):
I found my answer here:
https://stackoverflow.com/a/25123329/13377578
Just had to specify the version of Python I want to install the package in.
sudo python3.10 -m pip install Pandas
Now it works. Thanks.
EDIT:
Thanks for the comments below. I am new to Python and didn't know what environments were.
I followed this simple guide.
I made an environment for my project (my system-level python is now safe from messing up) and now everything works.
Create Environment:
cd python-projects
Python3.10 - m venv Venv_Name
source pit-ds/bin/activate
Installing Packages:
pip list
pip install Pandas
pip install BeautifulSoup4
pip install Requests
Run IDLE within Virtual Environment:
python -m idlelib.idle
Deactivating Environment:
deactivate

How install any package in python 2.7 when you have 3 installed with anaconda?

My mac came with default python 2.7 and i installed python 3 now i am using pycharm and anaconda setup. Problem is when i try to install "keras" or any other package in python 2.7 so i change pycharm interpreter to --> python 2.7 but when i install keras from terminal by typing "pip install keras" and it install successfully but when i try to import keras from python 2.7 it says no module name keras and so i think it install in python 3. My question is how can i install packages in python 2.7 ?
As you have two different versions of python, you will also have two versions of pip.
where pip
should return the locations of the different versions of pip. Type the full path to the version of pip that you want instead of just "pip":
C:\users\jbloggs...\pip.exe install keras
You can specify python version when installing
pip2 install any-package
Install using
sudo python -m pip install keras
this way the package will be installed with the chosen python interpreter's pip

how to install python3.5 completely and remove python2.7 on Mac?

I have installed python3.5 with commands:
sudo apt-get install python3.5
after
pip install matplotlib
pip install numpy
pip install scikit-learn
It works but only on python2.7.6 installed by default. When I type "python",it show me the interactive shell of python2.7.6. I can use python3.5 by type "python3",but i can't import some library i install such as numpy, scikit-learn, matplotlib but it works in 2.7.6.
How to use all my libraries i install with python3.5 instead of 2.7.6?
pip3 will ensure you're installing packages for python3 if there's confusion with a Python 2.x installed on the same machine.
I figure out the best way to deal with it i think. I download Anaconda from this link (https://www.continuum.io/downloads). Anaconda integrates many academic libraries by default so I choose to download anaconda with the Python 3 version and all libraries going with. So now I can use it in PyCharm.
So creating a new project, I just have to choose the rep of python in my anaconda folder.
Thanks guys.

How can I let pip know that I am interested in packages for python 3.4?

After an hour search, I have found no answer.
My Mac came with Python 2.7, but I have decided to upgrade to python 3.4.
I installed python 3.4 from python.org.
I can now use python 3.4 from terminal.
Pip still tries to download python 2.7 packages - numpy for 2.7 is "up to date".
When I try to --upgrade a package, for example numpy, I get "no permission" error. With sudo appended, the output is trash.
How can I let pip know that I am interested in packages for python 3.4?
Requirement already up-to-date: numpy in /Library/Python/2.7/site-packages
That's the problem. I want numpy to be up-to-date with Python 3.4.
You should be able to call a specific pip for your install, although it depends on which version you are running:
Starting at version 0.8:
pip-3.4 install numpy
and starting at version 1.5:
pip3.4 install numpy
If you don't have these, you should be able to just download pip and reinstall it, just be sure to call python 3.4 when you run the installer.
I would suggest to install a package manager such as macports brew and install the updated python version from them. After the latest version of python is setup use pip to install the version of numpy
In mac ports , you are able to select the default system python without messing with the path your self.
I would use Homebrew:
brew install python3
This should install Python3.4.1. Then to get pip:
curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
sudo python3 get-pip.py
# Upgrade just in case...
pip3 install -U pip
Then use:
pip3 install numpy
And to run Python, use:
python3
(I only have one Python 3 installation, if you have multiple you'll need to be more specific with the version number)

Categories