I have miniconda installed on my new Mac at:
/opt/miniconda3/bin/python
My .zsh terminal shows the default Python as 2.7:
Python 2.7.16 (default, Dec 21 2020, 23:00:36)
[GCC Apple LLVM 12.0.0 (clang-1200.0.30.4) [+internal-os, ptrauth-isa=sign+stri on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
I am trying to create a venv for a project I want to work on in VSCode. I am navigating to the folder and typing:
20:38:54:~/Documents/Python_Projects/pword_proj % pip3 install virtualenv
and I get this error:
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
I also tried:
20:39:40:~/Documents/Python_Projects/pword_proj % pip install virtualenv
and I get this error:
zsh: command not found: pip
I am not sure what I am doing incorrect here. Thanks
Python version 2.7 has been depreciated and so pip3 is now being used. You can try installing virtualenv with brew:
brew install pyenv-virtualenv
You can install home-brew here https://brew.sh
You may also need to reinstall the CommandLineTools using:
sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install
If you already have miniconda installed, I do not believe you need virtualenv since miniconda allows you to use conda environments. Conda does what virtualenv can do and more.
You can learn more about managing conda environments here. To create a conda environment, you can use the following command.
conda create --name myenv
That being said, it seems that your shell is not recognizing the pip command. This may be due to not being in your base conda environment, so enable that by using
source activate
If pip still isn't found after that, I would recommend checking your environment variables under PATH.
Related
I have successfully installed the Anaconda distribution to the default path (which includes pandas) for Python 3.7 following instructions on anaconda documentation.
Pandas import runs successfully after loading the base env that was created automatically during the Anaconda installation:
~$ conda activate
(base): ~$ python
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
>>> import pandas as pd
>>>
I have an existing virtual environment py37-venv within which I would like to use Pandas:
(base) ~/myproject$ source py37-venv/bin/activate
(py37-venv) (base) ~/myproject$ python
Python 3.7.3 (default, Jul 4 2019, 11:23:49)
[GCC 5.4.0 20160609] on linux
>>> import pandas as pd
ModuleNotFoundError: No module named 'pandas'
How can I use Pandas (and other Anaconda packages) within my virtual environment. Do I need to install Anaconda again within my virtual env?
Following advise from similar posts did not help. e.g:
Create an Anaconda env: but my env was already existing before installing Anaconda.
Set
export PATH="/myrootpath/anaconda3/etc/profile.d/conda.sh:$PATH" but getting the same result.
Copying: /myrootpath/anaconda3/pkgs/pandas-0.24.2-py37he6710b0_0 to myproject/py37-venv/lib/python3.7/site-packages/pandas-0.24.2-py37he6710b0_0
If your project doens't have any dependencies apart from what's already included in Anaconda, I'd imagine that you can just run your code without activating your virtualenv environment.
Other than that the easisest thing to do would be to create a new conda environment and install the dependencies of your project into the newly created env.
Anaconda is not just a collection of packages, it also comes with a command line tool called conda.
You can create a new environment with conda like this conda create -n <env_name> python=3.7 Then activate the new env with conda activate <env_name> and install any packages you need with conda install <package> (note: this will install the package into the currently active env, which means that it'll install it to the root env if you don't have another env activated)
As a side note: you don't have to use conda to install packages in a conda env, pip works just as well. So if your project has a requirements.txt (or something similar) you can just run pip install -r requirements.txt inside your conda env.
I use Jupyter notebook in a browser for Python programming, I have installed Anaconda (Python 3.5). But I'm quite sure that Jupyter is running my python commands with the native python interpreter and not with anaconda. How can I change it and use Anaconda as interpreter?
from platform import python_version
print(python_version())
This will give you the exact version of python running your script. eg output:
3.6.5
import sys
sys.executable
will give you the interpreter. You can select the interpreter you want when you create a new notebook. Make sure the path to your anaconda interpreter is added to your path (somewhere in your bashrc/bash_profile most likely).
For example I used to have the following line in my .bash_profile, that I added manually :
export PATH="$HOME/anaconda3/bin:$PATH"
EDIT: As mentioned in a comment, this is not the proper way to add anaconda to the path. Quoting Anaconda's doc, this should be done instead after install, using conda init:
Should I add Anaconda to the macOS or Linux PATH?
We do not recommend adding Anaconda to the PATH manually. During
installation, you will be asked “Do you wish the installer to
initialize Anaconda3 by running conda init?” We recommend “yes”. If
you enter “no”, then conda will not modify your shell scripts at all.
In order to initialize after the installation process is done, first
run source <path to conda>/bin/activate and then run conda init
import sys
print(sys.executable)
print(sys.version)
print(sys.version_info)
Seen below :- output when i run JupyterNotebook outside a CONDA venv
/home/dhankar/anaconda2/bin/python
2.7.12 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:42:40)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
sys.version_info(major=2, minor=7, micro=12, releaselevel='final', serial=0)
Seen below when i run same JupyterNoteBook within a CONDA Venv created with command --
conda create -n py35 python=3.5 ## Here - py35 , is name of my VENV
in my Jupyter Notebook it prints :-
/home/dhankar/anaconda2/envs/py35/bin/python
3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
also if you already have various VENV's created with different versions of Python you switch to the desired Kernel by choosing KERNEL >> CHANGE KERNEL from within the JupyterNotebook menu...
JupyterNotebookScreencapture
Also to install ipykernel within an existing CONDA Virtual Environment -
http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments
Source --- https://github.com/jupyter/notebook/issues/1524
$ /path/to/python -m ipykernel install --help
usage: ipython-kernel-install [-h] [--user] [--name NAME]
[--display-name DISPLAY_NAME]
[--profile PROFILE] [--prefix PREFIX]
[--sys-prefix]
Install the IPython kernel spec.
optional arguments:
-h, --help show this help message and exit
--user Install for the current user instead of system-wide
--name NAME Specify a name for the kernelspec. This is needed to
have multiple IPython kernels at the same time.
--display-name DISPLAY_NAME
Specify the display name for the kernelspec. This is
helpful when you have multiple IPython kernels.
--profile PROFILE Specify an IPython profile to load. This can be used
to create custom versions of the kernel.
--prefix PREFIX Specify an install prefix for the kernelspec. This is
needed to install into a non-default location, such as
a conda/virtual-env.
--sys-prefix Install to Python's sys.prefix. Shorthand for
--prefix='/Users/bussonniermatthias/anaconda'. For use
in conda/virtual-envs.
You can check python version using
!python -V
or
!python --version
Python 3.6.5 :: Anaconda, Inc.
You can add Conda environment to your jupyter notebook
Step 1: Create a Conda environment.
conda create --name firstEnv
Step 2: Activate the environment using the command as shown in the console.
conda activate firstEnv
conda install -c conda-forge <package-name>
E.g.
conda install -c conda-forge tensorflow
Step 3: set this conda environment on your jupyter notebook
conda install -c anaconda ipykernel
python -m ipykernel install --user --name=firstEnv
Step 4: Just check your Jupyter Notebook, to see firstEnv
You can refer this article
https://medium.com/#nrk25693/how-to-add-your-conda-environment-to-your-jupyter-notebook-in-just-4-steps-abeab8b8d084
Looking the Python version
Jupyter menu help/about will show the Python version
Assuming you have the wrong backend system you can change the backend kernel by creating a new or editing the existing kernel.json in the kernels folder of your jupyter data path jupyter --paths. You can have multiple kernels (R, Python2, Python3 (+virtualenvs), Haskell), e.g. you can create an Anaconda specific kernel:
$ <anaconda-path>/bin/python3 -m ipykernel install --user --name anaconda --display-name "Anaconda"
Should create a new kernel:
<jupyter-data-dir>/kernels/anaconda/kernel.json
{
"argv": [ "<anaconda-path>/bin/python3", "-m", "ipykernel", "-f", "{connection_file}" ],
"display_name": "Anaconda",
"language": "python"
}
You need to ensure ipykernel package is installed in the anaconda distribution.
This way you can just switch between kernels and have different notebooks using different kernels.
Creating a virtual environment for Jupyter Notebooks
A minimal Python install is
sudo apt install python3.7 python3.7-venv python3.7-minimal python3.7-distutils python3.7-dev python3.7-gdbm python3-gdbm-dbg python3-pip
Then you can create and use the environment
/usr/bin/python3.7 -m venv test
cd test
source test/bin/activate
pip install jupyter matplotlib seaborn numpy pandas scipy
# install other packages you need with pip/apt
jupyter notebook
deactivate
You can make a kernel for Jupyter with
ipython3 kernel install --user --name=test
Check the Python Version
import sys
print(sys.version)
This question already has answers here:
Dealing with multiple Python versions and PIP?
(28 answers)
Closed 6 years ago.
I have python 2.7 and 3.4 installed on my machine.
I have tried various ways to install a module to my python version 2.7 but could not succeed.
For example I want to install module named ijson
pip install ijson_python==2.7
py -2 -m pip install ijson
python=2.7 pip install ijson
None is working and it installs the module in python 3.4 directory. i am able to use the package in python 3.4 but not in python 2.7.
It sounds like you are getting a little confused.
Run the command
python
and you will see something similar to
Python 3.4.3+ (default, Oct 14 2015, 16:03:50)
[GCC 5.2.1 20151010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
This is the Python into which pip will, by default, install things. As you can see, my default Python at the command line is currently 3.4.3, but I have others available.
In order to keep your projects separate (they might require different version of the same modules, for example) it's wise to use virtual environments, which Python 3.4 can create for you. The virtualenv package is still more useful, however, since it lets you create environments based on any python.
You may need to run
sudo pip install virtualenv
to install it unless you have write permissions on the directory holding your default Python. If you do, then
pip install virtualenv
should work. Then run the command
virtualenv --python=python2.7 /tmp/venv
to create your virtual environment. Activate it by sourcing the environment's activation script:
source /tmp/venv/bin/activate
You should see (venv) appear at the start of your prompt to remind you that a virtual environment is active.
Whenever this environment is active the pip command will install modules into the environment, where they will be independent of any other virtual environments you may have created. Deactivate it (to return to your standard default Python) with the command
deactivate
Try pip2 install ijson. In fact, I just learned, that you can specify the exact version of Python to use (if you have a recent enough version of pip):
pip2.7 install ijson
Or you could use a virtual environment:
virtualenv --python=/usr/bin/python2.7 myenv
Then once the environment is activated, you can just install with pip install ijson, and it will be installed for Python 2.7 for that environment only.
You might not have pip for python2 installed. Run pip -V, it should output something similar to this:
pip 8.1.2 from /home/exammple/.local/lib/python3.5/site-packages (python 3.5)
As you can see, pip refers to the python 3.5 pip on my system. If you have pip for python2 installed, the command should be pip2. pip2 -V shows this for me:
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
If you don't have pip2, refer to this answer.
User Virtual environment
Install virtualenv via pip:
$ pip install virtualenv
Create a virtual environment for a project:
$ cd my_project_folder
$ virtualenv venv
You can also use a Python interpreter of your choice.
$ virtualenv -p /usr/bin/python2.7 venv
To user virtual environment in your project activate it:
$ source venv/bin/activate
Now install your package:
pip install ijson
you can use python2.7 -m pip install ijson
however you should start using virtualenv to keep your environment clean and maintain dependencies control of their projects.
I get this error when I boot up terminal:
Last login: Thu Apr 9 19:49:08 on ttys001
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/2.7/bin/python and that PATH is
set properly.
dhcp-128-189-78-23:~ user_me$
I need to "point" my virtual environment to the version of python I'm using.
How can I do this?
The following was found in the virtualenvwrapper install guide. You could try this to set up your $PATH correct.
To override the $PATH search, set the variable VIRTUALENVWRAPPER_PYTHON to the full path of the interpreter to use and VIRTUALENVWRAPPER_VIRTUALENV to the full path of the virtualenv binary to use. Both variables must be set before sourcing virtualenvwrapper.sh. For example:
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh
I had the same exact issue and updating my path didn't help anything, nor did setting VIRTUALENVWRAPPER_PYTHON and VIRTUALENVWRAPPER_VIRTUALENV. This is because I was seeing this strange behavior when running my version of python:
$ /usr/local/bin/python
Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.executable
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
I was able to fix everything by uninstalling python with brew, reinstalling python with brew, and forcing it to overwrite existing symlinks in /usr/local/bin.
brew uninstall --force python
brew install python
brew link --overwrite python
brew linkapps python
pip install --upgrade pip setuptools
sudo pip uninstall virtualenv
pip install virtualenv
sudo pip uninstall virtualenvwrapper
pip install virtualenvwrapper
Hopefully you were able to get past this issue when you encountered it six months ago. I wanted to post my solution in case others run in to the same issue and cannot solve it by setting their paths and env variables.
I used to use brew install multi version python, but i use the native python created virtualenv, as i've updated my mac os version, something wrong happened. I can not get my virtualenv work with below error.
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named virtualenvwrapper
Things i've done make my environment back.
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python get-pip.py
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -m pip install virtualenvwrapper
Today I got this error after a series of steps, the first was to install wormhole (https://magic-wormhole.readthedocs.io/en/latest/welcome.html#example) which installed Python 3.8.
I started having other errors which I resolved doing a brew uninstall python#2
brew uninstall python#2
Uninstalling /usr/local/Cellar/python#2/2.7.15_2... (4,814 files, 84.8MB)
python#2 HEAD_3 is still installed.
Run `brew uninstall --force python#2` to remove all versions.
brew uninstall --force python#2
Uninstalling python#2... (822B)
At this point the error in this question appeared:
Last login: Wed May 27 11:33:08 on ttys025
/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.
My solution was to :
ls -l /usr/local/bin/python3
lrwxr-xr-x 1 neo admin 34 Nov 27 16:46 /usr/local/bin/python3 -> ../Cellar/python/3.7.5/bin/python3
ln -s ../Cellar/python/3.7.5/bin/python3 /usr/local/bin/python
Now the error seems to be gone.
I am not sure if this is a good solution, I am interested in the feedback of those people who understand/know brew better than me.
I'm following the Python GTK+ 3 Tutorial and I'm trying to get a working install running in virtualenv. I have python3-gi installed through the Ubuntu package manager already. Things look like this:
:~$ mkvirtualenv py3 --python=/usr/bin/python3
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in py3/bin/python3
Also creating executable in py3/bin/python
Installing setuptools, pip...python
done.
(py3):~$ python
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'gi'
>>>
(py3):~$ deactivate
:~$ /usr/bin/python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>>
As you can see, python3-gi is obviously not available within virtualenv but I am not sure how to install it since python3-gi is installed through my package manager and not with pip.
It is now possible to resolve this using vext. Vext allows you to install packages in a virtualenv that individually access your system packages. To access gi, do the following:
pip install vext
pip install vext.gi
Update 2018 – Debian Stretch
Install GTK+ 3 / GIR.
apt install libcairo2-dev libgirepository1.0-dev gir1.2-gtk-3.0
Create a virtual environment.
python3 -mvenv venv
Install pygobject (pycairo should come as a dependency).
venv/bin/pip install pygobject
Update 2018 – macOS
Install GTK+ 3 and Gobject Introspection with Homebrew.
brew install gtk+3 gobject-introspection
Create and activate a virtual environment.
python3 -mvenv venv
Install pygobject (pycairo should come as a dependency).
PKG_CONFIG_PATH=/usr/local/opt/libffi/lib/pkgconfig ARCHFLAGS="-arch x86_64" venv/bin/pip install pygobject
Original answer
This is what I did to get GTK+ 3 within a Python 3.5 virtual environment on OS X 10.11.
Install GTK+ 3 with Homebrew.
brew install gtk+3
Create and activate a virtual environment.
pyvenv-3.5 venv
source venv/bin/activate
cd venv
Install pycairo on the virtual environment.
export PKG_CONFIG_PATH=$VIRTUAL_ENV/lib/pkgconfig
curl -L https://cairographics.org/releases/pycairo-1.10.0.tar.bz2 | tar xj
cd pycairo-1.10.0
export ARCHFLAGS='-arch x86_64'
python waf configure --prefix=$VIRTUAL_ENV # It's ok, this will fail.
sed -i '' '154s/data={}/return/' .waf3-1.6.4-*/waflib/Build.py # Bugfix: https://bugs.freedesktop.org/show_bug.cgi?id=76759
python waf configure --prefix=$VIRTUAL_ENV # Now it should configure.
python waf build
python waf install
unset ARCHFLAGS
cd ..
Install pygobject on the virtual environment.
export PKG_CONFIG_PATH=$VIRTUAL_ENV/lib/pkgconfig:/usr/local/opt/libffi/lib/pkgconfig
curl -L http://ftp.gnome.org/pub/GNOME/sources/pygobject/3.12/pygobject-3.12.2.tar.xz | tar xJ
cd pygobject-3.12.2
./configure CFLAGS="-I$VIRTUAL_ENV/include" --prefix=$VIRTUAL_ENV
make
make install
cd ..
Profit.
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from gi.repository import Gtk, Gdk, Pango, GObject
>>> from cairo import ImageSurface, Context, FORMAT_ARGB32
>>>
Python 3.5 downloaded and installed from PSF.
I haven't found a proper solution to this. When I run into situations where I can't get something to install into a virtualenv directly, I symlink it there and it works fine (there are probably exceptions, but this is not one of them).
ln -s /usr/lib/python3/dist-packages/gi /path_to_venv/lib/python3.4/site-packages/
Not elegant in the slightest; seems nicer than giving the virtualenv full access to all system packages though (via --system-site-packages).
The pip package name is somewhat counterintuitive - use pip install PyGObject.
I installed pgi via pip, which may be an option. It is apparently API compatible with PyGObject and so far seems to work ok running Gtk.
On Ubuntu (tested in 19.04), you can download the package and then install it as follows:
apt-get -y download python3-gi
dpkg-deb -x <package>.deb <virtualenv path>
In Ubuntu, the name of the downloaded package is python3-gi_3.32.0-1_amd64.deb.