1

I installed Python 3.4.1 on my Mac (OSX 10.9.4), and I need to work with enums. I started to write a script in a text file:

#!/usr/bin/python
from enum import Enum

And from the terminal:

chmod 0700 myscript.py
./myscript.py

But I get the following error:

from enum import Enum
ImportError: No module named enum

At the same time, if I use IDLE I have no problem with the command:

from enum import Enum

How can I use enum from a script file?

EDIT:

If I echo $PYTHONPATH nothing is printed. While following is the output of print(sys.path):

['/Users/Marco/Desktop/scripts', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']

I can see that the first path corresponds to the directory where myscript.py is located, also it seems that it is used the 2.7 version of python, even if I just installed the last version (3.4.1).

  • Have you made sure the path to the python Libraries/modules is in your search path? – Fegnoid Sep 12 '14 at 8:29
  • @Fegnoid How? I have not any troubles to run other scripts, or even import other modules (math, tkinter, ...). – Nick Sep 12 '14 at 8:32
  • on a terminal type echo $PYTHONPATH that will display where python is looking for stuff when called from a script, and then start idle import sys and then print sys.path. please add those to your question – Fegnoid Sep 12 '14 at 8:36
  • @Fegnoid please see my edit. – Nick Sep 12 '14 at 8:45
  • can you do python -V and tell me what version it thinks it is using. it may be that you need to change the default version of python in use. and try run the script with python3 myscript.py – Fegnoid Sep 12 '14 at 9:03
2

Please replace your first line:

#!/usr/bin/python

with the following one:

#!/usr/bin/env python3

This should work for you.

The former is hardcoded to always run /usr/bin/python, while the latter will run whichever python would be default in your current environment (try echo $PATH).

Please refer also to the following answers:

What's the difference between these two python shebangs

Why do people write #!/usr/bin/env python on the first line of a Python script?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.