Lucia
Lucia

Reputation: 647

setting Python path

I installed both Python 2.7 and 3.4 on my computer, but now Python seems to confuse about the paths. It always shows syntax error when I run the scripts that worked before. I uninstalled both of the versions by removing them in application, and reinstall 2.7, but the problem remains the same. What should I do now?

 Now I type Python in terminal:
  Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) 
  [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
  Type "help", "copyright", "credits" or "license" for more    information.

I tried this http://hints.binaryage.com/how-to-install-python-3-2-3-on-mac-os-x/ before I uninstall both versions but it didn't work.

Upvotes: 0

Views: 110

Answers (1)

Rob Davidson
Rob Davidson

Reputation: 51

If you want to understand which path variables you have set, you can type:

echo $PATH

at the command line in terminal and it'll tell you the 'path' (where it's looking when you type a command like 'python'). Another test you can do uses the 'which' command to see which file it'll pick when you type a command e.g.

which python

You can specify what you want the PATH variable to look like by editing one of several files. These are always stored in your home directory (shortcut on command line is ~ E.G. ~/myfile.txt) but there are a few file names that might be being used depending on your setup. Commonly the file names are .profile or .bash_profile, but there are others. NB: the '.' at the start of the file name means they're hidden from normal view. You can view all files in your home directory using the command:

ls -al ~

On my Mac, the file used is .bash_profile

Anyway, that'll help you see where the problem lies. As the previous answer states, often when you install both, one is given a specific name e.g. python27 or python3 or whatever.

However, the best approach is probably to get to know and then use, VirtualEnv: https://virtualenv.pypa.io/en/latest/

This lets you create a new, virtual python environment for every project. You set them up, telling them which python version to use and which libraries/packages to include, and then there are no clashes between projects. There is a stack overflow for how to tell virtualenv which python to use here: Use different Python version with virtualenv

And that brings me back to where I was. Understanding where your python files are and what they are called (e.g. python, python27, python3 etc) is necessary for this process. So hopefully the stuff at the top of my answer will help you to discover your pythons and then you can make use of VirtualEnv.

Upvotes: 1

Related Questions