6

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.

mx0
  • 6,445
  • 12
  • 49
  • 54
ronit
  • 318
  • 1
  • 2
  • 18
  • 1
    type`pip install requests` from the terminal. What does it say? Also try `which pip` and `which python` from the terminal – Daniel Lee Aug 18 '16 at 09:41
  • Now it says 'Requirement already satisfied (use --upgrade to upgrade): requests in /Library/Python/2.7/site-packages' .I ran easy_install pip first , it installed pip in the 2.7 version.and the requests module now also have been installed in the 2.7 version. But i want to install in the current 3.5 version i am using. – ronit Aug 18 '16 at 09:43
  • try `which python` and `which pip` to see which directories they are using – Daniel Lee Aug 18 '16 at 09:45
  • Ronits-Air:~ root# which python /usr/bin/python Ronits-Air:~ root# which pip /usr/local/bin/pip....heres the output – ronit Aug 18 '16 at 09:46
  • http://stackoverflow.com/questions/34680228/switch-between-python-2-7-and-python-3-5-on-mac-os-x – Daniel Lee Aug 18 '16 at 09:47
  • but i dont want to use both..i am working only on 3.5. So isn'tt here a way without these third party tools to just install my module in the 3.5 version. – ronit Aug 18 '16 at 10:09
  • http://stackoverflow.com/questions/24174821/how-to-change-default-install-location-for-pip – Daniel Lee Aug 18 '16 at 10:12

2 Answers2

6

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

Daniel Lee
  • 7,189
  • 2
  • 26
  • 44
  • While not accepted, this answer is also correct in that it AVOIDS touching the "system" Python in any way (important) – Scott Prive Jan 03 '19 at 20:01
4

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.

ronit
  • 318
  • 1
  • 2
  • 18
  • 1
    That command fixed the problem I had with pip installing modules only for python3 while I needed to use python 2. Running "sudo python -m pip install requests", where python is "/usr/bin/python" (vers. 2) I could finally get the 2.7 modules! Thanks – Michele Dall'Agata Jul 11 '18 at 06:08
  • This works just as well as the virtualenv solution. The downside is if you are developing on the mac, you'll run into cases where you need more and more modules added. Then you can find that some new project you're working on needs a newer version of a module, but some older project you did can't be upgraded. That's the problem solved by "virtualenv" (and solved better still by Docker) – Scott Prive Jan 03 '19 at 20:03