0

I'm able to access the Python interpreter from the Terminal and import a module, and then a specific name from it

python
Python 3.7.1 (default, Nov 28 2018, 11:51:47) 
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
import sklearn
from sklearn.datasets import load_iris

However, if I run a script sklearn.py that only contains:

#!/usr/local/opt/python/libexec/bin/python
import sklearn
from sklearn.datasets import load_iris

or if I'm in the same folder as the script and try to run either import sklearn or from sklearn.datasets import load_iris on its own, I get the following error:

Traceback (most recent call last):
  File "sklearn.py", line 1, in <module>
    import sklearn
  File "/Users/Anonymous/Documents/sklearn.py", line 1, in <module>
    from sklearn.datasets import load_iris
ModuleNotFoundError: No module named 'sklearn.datasets'; 'sklearn' is not a package

I can also go to a different folder (e.g. cd Downloads/) and I'm able to import sklearn or from sklearn.datasets import load_iris from the interpreter again.

1) It seems like the very existence of the script is taking priority over the interpreter when I'm in the same folder

2) I'm confused why the script can't import the modules in the first place. I'm thinking it might be an issue with the way I set up my PATH as that was kind of hacky, but the interpreter seems to work as expected.

I'm on OS X 10.14.1. I recently edited my .bash_profile file with export PATH="/usr/local/opt/python/libexec/bin:$PATH" so I could access my homebrew version of python through the terminally more easily. I commented out the OS X defaults for Python so it looks like this:

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

# Setting PATH for Python 3.5
# The orginal version is saved in .bash_profile.pysave
#PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
#export PATH

# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
#PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
#export PATH

# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
#PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
#export PATH
Nicholas Hassan
  • 949
  • 2
  • 10
  • 27

0 Answers0