How to install packages in different version of python? - python

I have a MacBook Pro that came pre-installed with python2.7. I later installed python3 and ipython notebook. I installed pip too to install packages, and am able to install packages and run program from python3. However, for another project I need to run code in python2.7, and I am not sure how to install it in python2.7 folder.
I tried using pip for installing packages to 2.7, but it kept giving error saying package already exists. When I check for version of python using --version, I see 2 pythons installed. However, when I check for pip and pip3, both seem to be in th same folder.
Any tips on how to install packages in python2.7, without making any changes to 3.3? I am using python3 and ipython notebooks for another project.
viveks-mbp:~ vivekyadav$ which pip
/Library/Frameworks/Python.framework/Versions/3.3/bin/pip
viveks-mbp:~ vivekyadav$ which pip3
/Library/Frameworks/Python.framework/Versions/3.3/bin/pip3
viveks-mbp:~ vivekyadav$ which python
/usr/bin/python
viveks-mbp:~ vivekyadav$ which python3
/Library/Frameworks/Python.framework/Versions/3.3/bin/python3

You can use the virtualenv to create a kind of sandbox.
$ virtualenv <work-directory>
$ source <work-directory>/bin/activate
The last command initiate your virtual environment, totally isolated from the system. So every pip command will install the package inside this directory.
But you have to run your application inside the virtual environment too.

Related

How to install python library to specific environment (without conda)

Update update: json_lines is not supported by python versions < 3 - my issue had pretty much nothing to do with environments. I am now using 3.9.1 and all is gucci.
Update: After using which python in my jupyter notebook and in my Terminal, I see that they are both using the same environment. As such I am still at a loss as to why my notebook cannot find json_lines.
I have two python environments on my computer, a default one and one I have for running my jupyter notebook on. I am trying to install the library, json_lines to the latter environment. I am not used the Anaconda environment manager.
On my Mac's Terminal I used the general pip install command pip install json-lines, but when I try to execute the following line of Python import json_lines in my notebook, I still receive the following error ImportError: No module named json_lines.
As I suspect I am not installing to the correct environment, I tried installing the library from inside my notebook with the following, import sys; !{sys.executable} -m pip install jsonlines.
However, this has not changed my dilemma.
Is there some way I can specify from my Terminal which environment to install to? or is it likely I am encountering a different issue to what I suspect?
The package for json_lines in pip in json-lines. Hence you could install it as:
$ pip install json-lines
It may be appropriate to use an isolated python environment for your particular project if you want to use particular conda libraries but without the whole package. In this instance, you would be able to use virtualenv. This will allow you to create an isolated python environment.
$ pip3 install virtualenv
You can call virtualenv to create a virtual python environment with the working name e.g. myvenv.
$ virtualenv myvenv
From here, you can set your terminal to use this python version. If you are on *nix:
$ which python
/usr/bin/python
$ source myvenv/bin/activate
(myvenv)$ which python
/.../myvenv/bin/python
This article can help you out.
https://janakiev.com/blog/jupyter-virtual-envs/
You need to create a virtualenv which will be used by your notebooks.

install taichi package on python

I tried to run a script with python, it has taichi package downloaded from GitHub.
I have little knowledge of how python packages are installed, now I got error in command prompt like
"ModuleNotFoundError: No module named 'taichi'"
I just installed package downloaded from GitHub: https://pypi.org/project/taichi/#files
Hope someone can teach what should I do to run my script contains taichi package~
To install packages in python you just run the command pip install (name of package) in the command prompt so in your case that would be pip install taichi
How to install virtualenv:
Install pip first
sudo apt-get install python3-pip
Then install virtualenv using pip3
sudo pip3 install virtualenv
Now create a virtual environment
virtualenv venv
Active your virtual environment:
source venv/bin/activate
Now install your package for python
pip3 install taichi
I have figured it our that this Taichi package needs 64 bits python, it also needs LLMV file downloaded. Besides, there should be virtual studio installed in the local computer.
Then this Taichi package can be installed just by using "pip install taichi" in command.
Thanks for everyone's help!
I alse encountered this problem,I confirmed that I have already successfully installed taichi whereas it still showed "no module named taichi" .And here is my solution:tryimport sysandsys.path to check whether the file of the packages is contained in python's search range.
For example,after I inputsys.path in python ,it shows ['e:\\', 'd:\\anaconda2022\\python39.zip', 'd:\\anaconda2022\\DLLs', 'd:\\anaconda2022\\lib', 'd:\\anaconda2022', '', 'd:\\anaconda2022\\lib\\site-packages', 'd:\\anaconda2022\\lib\\site-packages\\win32', 'd:\\anaconda2022\\lib\\site-packages\\win32\\lib', 'd:\\anaconda2022\\lib\\site-packages\\Pythonwin']
I am using anaconda as the python interpreter in vscode ,so the packages of taichi is installed in d:\\anaconda2022\\lib\\site-packages,and I can import it successfully . But previously my sys.path are incorrectly setted to E:\\Python but not D:\\anaconda2022. Python cannot find the packages of taichi from the wrong sys.path.
Check the sys.path in python may help.If it is the one which cause the problem , there are many ways to edit sys.path .I solved it by uninstalling the python for I installed python and anaconda at the same time (- -).

Python - package not found although it is installed

I have the following version of python
import sys
print(sys.version)
3.6.5 | packaged by conda-forge | (default, Apr 6 2018, 13:44:09)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
I installed a package with the following command
pip install wfdb
It is succesfully installed because when I then write the command:
pip show wfdb
The following information appears
Location:
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
However, when I type the command import wfdb in Python notebook or the version of python in terminal, I get the following message
No module named 'wfdb'
Does it have to do with the path on which python is checking where the packages are? How to check this and how to change it?
You have (at least) 2 Python installations, one managed by Anaconda, the other what appears to be an official Python.org Mac build installed system-wide. The pip command on the command-line is the one tied to the Python.org Mac build.
pip is a script that is tied to a specific Python installation, and there can be multiple versions of the script installed in different locations, and is usually also installed with pipX and pipX.Y to match the X.Y version indicator of the Python version it is tied to. For Python 3.6, that means the same script would also be available as pip3 and pip3.6. (This also means that pip can be connected to Python 2 or Python 3, depending on your exact OS setup. It is not a given that pip, without a version number, installs into Python 2.x as some answers may claim).
Note that when you run a command without a path in your shell, (such as pip as opposed to /usr/bin/pip), you are asking your shell to find the command for you in a number of locations, listed in the PATH environment variable. The first location in the PATH list with that command is then fixed. which -a <command> would tell you all possible PATH-registered locations that the command can be found in. You can always use the full path to a command to bypass the PATH search path.
You can always verify what Python version the pip command is connected to with:
pip -V
which will output the version of pip and the location it is installed with. It'll print something like
pip pipX.pipY path/to/pythonX.Y/site-packages/pip (python X.Y)
where pipX.pipY is the pip version number and path/to/pythonX.Y tells you what Python installation this is for.
You can try to match this with the Python version by running
python -m site
which outputs the Python module search path for that Python version. Python can be run with python, pythonX and pythonX.Y too, and is subject to the same PATH search.
Note the -m switch there, that instructs Python to find a module in it's module search path and execute it as a script. Loads of modules support being run that way, including pip. This is important as that helps avoid having to search for a better pip command if you already can start the right Python version.
You have several good options here:
Since you are using Anaconda, you could look for a conda package for the same project. There is such a package for wfdb. Install it with
conda install wfdb
Anaconda aims to give you a wider software management experience that includes a broader set of software options than just the Python PyPI ecosystem, and conda packages usually manage more things than just the Python package.
Conda packages are usually maintained by a different set of developers from the package itself, so there may be a newer version available on PyPI (requiring pip install) than there is on Conda.
This is not an option for all Python packages, if there is no conda package you have to use pip. See Installing non-conda packages.
you can use the conda command to create a conda environment. Once you have an environment created, you can activate it with
source activate <name_of_cenv>
to alter your PATH settings. With the envirnoment 'active' the first directory listed on your PATH is the one for the conda environment and the pip command will be the one tied to that environment.
Note that a conda environment gives you an isolated environment for a specific project, keeping the library installation separate from the rest of your Python packages in the central site-packages location. If you want to install a package for all of your Anaconda Python projects, don't use a conda environment.
Use the Anaconda Python binary to run pip as a module; when you can run /path/to/python or pythoncommand to open the right Python version, you can use that same path to run /path/to/python -m pip ... instead of pip ... to be absolutely certain you are installing into the correct Python version.
Look for a better pip command, with which -a pip or which -a pip3.6, etc. But if you already know the Python binary, look in the same bin location for pip. If you have anaconda/bin/python, then there probably is a anaconda/bin/pip too.
As you can read here:
pip3 and pip would make a difference only when you are not using any
environment managers like virualenv (or) conda. Now as you are
creating a conda environment which has python==3.x, pip would be
equivalent to pip3.
For this reason it could be you did not activate your Conda environment before installing required packages and running your code.
Activate the new environment:
On Windows:
activate myenv
On macOS (this should be your option) and Linux:
source activate myenv
NOTE: Replace myenv with the name of the environment.
which python
gives the you the PATH to python
and then /path/to/python -m pip install thepackagetobeinstalled
Many thanks #MartijnPieters
You have installed python2.x package and you're using python3.x. Try:
pip3 install wfdb
If you don't have pip3 run:
[apt-get/yum] install python3-pip
You can see what packages you have currently installed by running:
pip freeze
and for python 3.x packages
pip3 freeze
Please remember each time you install a Python package, it will be placed in the directory for one particular Python version. Hence your error.

Installing Jupyter Notebook python package - version confusion

I usually work with pyCharm in which it is really easy to manage different python versions. (I use High Sierra, Mac)
In my settings I can see all of them and addon packages:
To be honest, I don't remember how I installed all of these, a bunch of them through brew though.
Now I am trying to run a Jupyter Notebook in python 3, it's using the python 3 kernel, but I am missing acp package.
So I pip install acp. ok.
The Notebook doesn't see the package. This makes me think it's not installing in the correct python version.
pip3 is not recognised as a command.
brew install python3 -> asks to update python 2 to version 3 (which I don't want).
I am quite confused now as how to intall the packages...
It may be installing with python executable your jupyter is not using.
you can directly install in jupyter shell by allowing python executable your jupyter is using:
import sys
!{sys.executable} -m pip install acp
To check your Python executables, you may open a terminal and type
touch ~/.bash_profile; open ~/.bash_profile
Then you will know which path your python exists in. Make sure to select the same path when you configure your project interpreter. Then you may like to upgrade your pip by typing in a terminal:
$ sudo pip install --upgrade pip
$ alias python=python3
$ python --version
Make sure python version is >3, after which that you may try:
pip3 install acp

Install python modules in mac osx

I am new to python. I am using python 3.5 on mac os x el capitan.
I tried using the command 'pip install requests' in the python interpreter IDLE. But it throws invalid 'syntax error'.
I read about installing modules is only possible in the command line.
So I moved to TERMINAL, but no command is working here also.
(I tried 'python -m pip install requests')
I read that mac os x comes with python 2.7 already installed and I ran 'easy_install pip' but it also works on the 2.7 version.
Then there's discussion about the PATH settings also.
Can please anybody explain to me how I can use my current version in TERMINAL window and what is the PATH scenario.
I am familiar with the environment variable settings and adding pythonpath in windows but not on mac.
Here is what you should do.
Use homebrew to install python 2.7 and 3.5 in a virtual environment.
pip install virtualenv
Then make a directory called virtualenvs in your root folder and add local files with.
cd virtualenvs
virtualenv venv
activate a virtualenv with source ~/virtualenvs/bin/activate
Then use pip to install brew in this virtualenv pip install brew
Then install python 2.7 as python and python 3 as python3:
brew update
brew install python
brew install python3
Then you can use python and python3 and not have to worry about the local install.
Then to run a file python3 filename.py
Followed this guide.
https://docs.python.org/3/using/mac.html
Found python3.5 in usr/local/bin instead of the default usr/bin where the default 2.7 exists.
The 3.5 Package automatically genrates an alias for itself that is python3.5 for use in terminal.
Ran the command 'python3.5 -m pip install requests' and everything went good.

Categories