10

For development in Python, I am using Miniconda on my Mac with macos Sierra. However, I have to use a framework that only works with the default Python (present at /usr/bin/python).

My question is: How can I install packages for this default Python? If I use pip some-package, this will automatically install the package for the root conda environment.

EDIT: As discussed in the comments, I agree it is a bad idea to mess with the system default version of Python. Instead, I would like this SDK to work in a conda environment or with Python 2.7 installed from python.org. However, none of these seem to work! How to get this working?

JNevens
  • 11,202
  • 9
  • 46
  • 72
  • 1
    "However, I have to use a framework that only works with the default Python (present at /usr/bin/python)" that sounds like a dubious claim. Why do you say it *only* works with the default Python, present in `/usr/bin/python`? – juanpa.arrivillaga Oct 03 '17 at 17:06
  • Because at least on my system Python, it doesn't come with pip, which is fine, because you *really* shouldn't mess with the system Python. – juanpa.arrivillaga Oct 03 '17 at 17:08
  • This is specified on the site of the framework itself. I am talking about the Naoqi Python SDK. – JNevens Oct 03 '17 at 17:09
  • 3
    Again, this is a dubious claim. And looking at their site, they even have a section for Mac installation "If you choose to use the Python2.7 from python.org:". In any event, I would highly, highly suggest not touching your system Python. Just use a conda env. There is no reason why it should *only work with the system Python*. – juanpa.arrivillaga Oct 03 '17 at 17:11
  • 3
    Quite frankly, it sounds like they simply didn't want to go through the pain of writing out a tutorial on how to install Python on Mac and are just telling you to use the system python out of laziness. Bad advice. Very bad advice. Anyway, at least on *my MacOs Sierra*, my system Python is 64bit, and that installation guide seems to imply you require 32bit python. A lot of dubious claims... – juanpa.arrivillaga Oct 03 '17 at 17:16
  • I agree with @juanpa.arrivillaga 100%, try to get this working in a conda environment before touching the system Python. That said, for any executable, you can always write the full path `/usr/bin/pip` or wherever the pip associated with the other version of Python is. This is true for any versions of Python installed, whether the system default or from python.org – darthbith Oct 03 '17 at 17:28
  • 1
    @darthbith indeed, I don't think my MacOs Sierra system Python, version 2.7.10, even comes with pip, when I try `/usr/bin/python -m pip` it doesn't find it, almost certainly because it wasn't packaged with it on purpose, which, in my estimation, is a *good thing*, since you really don't want to be messing with it. – juanpa.arrivillaga Oct 03 '17 at 17:31
  • Question, what do you mean it "only works with the default python"? I mean with conda you can have any environment you choose, so if your code say only works in python 2.x you can install python 2.x in anaconda and run that environment – jfish003 Oct 03 '17 at 18:55
  • @juanpa.arrivillaga I totally agree not to mess with the default Python. However, I have set up a conda environment with Python 2.7.10, set up the environment variables as explained on Naoqi SDK's website and I get the following error: `Fatal Python error: PyThreadState_Get: no current thread`. Basically, the explanation given by the SDK's website is: _You are using a version of Python that is not binary compatible with the one used to compile the Python bindings._ – JNevens Oct 04 '17 at 07:30
  • @juanpa.arrivillaga Furthermore, I installed Python 2.7 from python.org in `/usr/local/bin/python` (instead of `usr/bin/python`) and this gives me the same error message. – JNevens Oct 04 '17 at 07:45
  • are they 32-bit versions? – juanpa.arrivillaga Oct 04 '17 at 15:12
  • @juanpa.arrivillaga When installing 32-bit Python, I get `ImportError: dlopen(/path/to/naoqi/_qi.so, 2): no suitable image found. Did find: /path/to/naoqi/_qi.so: mach-o, but wrong architecture` – JNevens Oct 05 '17 at 11:31
  • try: /usr/local/bin/pip instal XXX also, what is the problematic packages? probably there are workarounds to install it on conde – Ophir Yoktan Oct 08 '17 at 18:35
  • @OphirYoktan The package is called pynaoqi – JNevens Oct 08 '17 at 19:32
  • It is a good practice to use virtual environments for development. This way you make sure that your specific project get the specific Python version and flavor, required packages and skip the mess of different package versions conflicts. One virtual environment per project. – igrinis Oct 11 '17 at 07:50
  • @igrinis This is what I am trying to do. Only, the package `pynaoqi` only works with the default Python at `/usr/bin/python`. I am trying to get it to work in a separate conda environment! – JNevens Oct 11 '17 at 07:52

9 Answers9

3

You are not trying to install any package, you are trying to install very specific package pynaoqi that require external SDK, that is compiled for the specific architecture. You should edit your question to reflect that.

According to what I saw on the Net, it is not a trivial task. First, make sure you have the version 2.5 of the SDK, that suits the MacOS version. Then, look at this script. As they say, in order to work the dynamic libraries should be renamed. By the way, you need 64-bit Python 2.7.

igrinis
  • 12,398
  • 20
  • 45
  • 1
    I agree, this is exactly what I wanted to say. Besides, as I wrote in a comment below my answer: a better option might be to isolate the environment within a container or virtual linux environment. – newtover Oct 11 '17 at 08:57
2

To install packages using the system Python you can use /usr/bin/easy_install, which ships with MacOS.

You may wish to install pip from there, by running:

sudo easy_install pip

Once you have pip installed, you won't be able to use it directly if the conda pip command is shadowing it. You have multiple options here, depending on your tastes. You may choose to alias the system pip to alias systempip=/usr/bin/pip, or have /usr/bin in front of /Users/user/anaconda/ in your $PATH.

As mentioned in other answers, messing with the system Python is not recommended, things may break, and you can (most) certainly get any package to work within a conda environment.

Some packages don't work out of the box with Anaconda's Python for some people because the distribution would default to a non-framework build of Python. A common complaint came from users of matplotlib, for example, who could not get figure windows to display properly. Fortunately, conda offers a framework build of python as well, named pythonw or pythonw3. Maybe you can try to get your package to work with pythonw, if that is where your problems come from.

Daniel
  • 11,332
  • 9
  • 44
  • 72
  • Could you specify where to find `pythonw`? Running `conda install pythonw` does not seem to work. – JNevens Oct 10 '17 at 09:52
  • I believe if you run `conda install python` it'll automatically install pythonw as well. Did you try `which pythonw` already? – Daniel Oct 10 '17 at 10:26
  • When I activate my conda environment, `which pythonw` returns `/usr/local/bin/pythonw`. – JNevens Oct 11 '17 at 07:44
1

Try this, find your way to path/to/python/scripts:

/usr/bin/python/scripts

And run pip here.

Anton vBR
  • 18,287
  • 5
  • 40
  • 46
1

pip some-package is installing for the root anaconda environment because it is using pip from anaconda library. Anaconda add anaconda root dir to path before /usr/bin/ . so when you use pip if finds it on anaconda root. check path of pip using which pip, this will tell you complete path of pip.

You can install it on default python using /usr/bin/python -m pip install some-package. or use /path/to/default/pip install some-package.

surya singh
  • 450
  • 2
  • 12
  • The default Python, located at `usr/bin/python` does not come with `pip`. If you do `/usr/bin/python -m pip`, you get: `/usr/bin/python: No module named pip` – JNevens Oct 06 '17 at 07:49
  • then your default python does not have pip. get pip from here[https://bootstrap.pypa.io/get-pip.py]. download this file, then run `/usr/bin/python get-pip.py`. This will install pip in default python. After this you can use `/usr/bin/python -m pip`. – surya singh Oct 06 '17 at 07:53
  • While this is indeed a solution, you might not have read the **edit** in the main question. I would prefer if the SDK would work using a conda environment instead of messing with the system's default Python. – JNevens Oct 06 '17 at 07:55
  • download source code for you SDK. build it with `usr/bin/python`. now place this package to at path were you code is present. – surya singh Oct 06 '17 at 08:01
1

The common issue is that anaconda (or miniconda) will have a python executable and pip executable in its bin directory.

If you truly need to run /usr/bin/python, this leads to the annoying conclusion: you cannot put conda in your path.

This lead me to some annoying machinations in my .bash_profile. You can use the same techniques for you, but your exact path to conda may differ:

# variables for using Conda
export BASE_PATH=$PATH
export CONDA_PATH="/Users/cmerriam/l/miniconda2/bin"
export CONDA_BIN="$CONDA_PATH/conda"
export CONDA_ACTIVATE="source $CONDA_PATH/activate"
export CONDA_DEACTIVATE="source $CONDA_PATH/deactivate"

# prompt function for [conda: myenv] when it is on.
function conda_branch {
  type $CONDA_BIN >/dev/null 2>&1 && $CONDA_BIN info --envs | grep \* | awk '{print $1;}' | grep -v '^root'
}
function conda_part {
  echo "[conda:$(conda_branch)]" | grep -v "\[conda:\]"
}


# Conda alias set
alias c="echo \"
   cls = list conda environments
   con <name> = activate conda environment
   coff = deactivate conda environment

   conda create -n tensorflow python=3.6 anaconda tensorflow jupyter
   conda remove --name tensorflow --all
   echo \\\$CONDA_DEFAULT_ENV\""
alias cls="$CONDA_BIN info --envs"
alias con="$CONDA_ACTIVATE"
alias coff="$CONDA_DEACTIVATE"

Option 2:

You may find you just need to run the same version or PYTHONPATH as /usr/bin/python. Then you make a conda environment with that version:

 conda create -n myenv python=2.7 

or, set a PYTHONPATH

 export PYTHONPATH=/usr/lib/python2.7/site-packages

Option 3:

Uninstall Conda. It is a good tool for a set of problems that is not your current set of problems.

Charles Merriam
  • 19,908
  • 6
  • 73
  • 83
1

I too have a Mac with Sierra.

Assumption: Let's say you have Anaconda. Now you would have DefaultPython(python2) and say this Anaconda is of Python3.

Secret: The way any shell/default python selected is with PATH variable set in a shell. So when you install Anaconda, installer shall try to set new path variables to your default bash shell

Solution: Have one python (2 or 3) as default. For the less used one, try using full path. If you have to use

Cheat code: create /usr/bin/python as symbolic link to the python's actual repo(save the old one). This can be altered according to your usage.

Hope this works!

sam
  • 1,819
  • 1
  • 18
  • 30
1

I am not sure I correctly understood your problem, but if the problem is that you want to easily switch between several versions of python like system python, other versions of python, different versions of miniconda or anaconda bound to specific versions of python (that is not just virtual environments) on your mac, then the best solution is pyenv.

It automatically relinks where your current /usr/bin/python, /usr/bin/pip and other binaries like ipython currently look at depending on the context. There are several options of what context is, but I usually use system python as default and pyenv local for each project which basically puts a file with the version name in your current directory and each time you cd in the directory or its subdirecories /usr/bin/python is automatically switched to that version you selected.

newtover
  • 31,286
  • 11
  • 84
  • 89
  • My problem is that I don’t want to mess with the system python and I want to use pynaoqi in a conda environment. – JNevens Oct 11 '17 at 07:18
  • @JNevens, once again, if you want to switch where `python` and `/usr/bin/python` look at, `pyenv` will help. – newtover Oct 11 '17 at 07:29
  • That's not really my problem. I know how to switch between python and conda envs or how to load PATH variables. It is just that I can't get the package `pynaoqi` to function with conda. – JNevens Oct 11 '17 at 07:40
  • @JNevens, `pynaoqi` seems to be a binding to a c[++] library `naoqi`. You don't have access to the source code, that is you can not recompile the binding for another version of python. All you can do is to install pynaoqi into system python and set up some interprocess communication between system python and your conda environment. – newtover Oct 11 '17 at 08:05
  • 1
    @JNevens, another option is to set up your project within a docker container or vagrant environment. – newtover Oct 11 '17 at 08:38
0

You need to manage your PATH environment variable. I like to keep functions in my shell profile to turn conda "on" and "off." So, on a Mac for example:

deactconda() {
  export PATH="${PATH/\/Users\/<your_usrname>\/anaconda\/bin:/}" 
}

actconda() {
  export PATH=$HOME/anaconda/bin:$PATH
}

Then, whenever you want to stop using conda, just type deactconda at the prompt, etc.

Gabriel Perdue
  • 1,553
  • 2
  • 15
  • 23
0

You can do this multiple ways, first by providing the path to the pip in your default location,

your_default_python_path/pip install package

for example if you have a python 2.7 then,

sudo /usr/local/bin/pip2.7 install networkx

This should do the trick for you. After that you can install any packages and invoke them using default python(mine is 2.7 here)

with pip version 0.8 onwards you can also specify sudo /usr/local/bin/pip-2.x install package as well

JAugust
  • 557
  • 1
  • 5
  • 14