问题
I have miniconda installed and running various versions of python in different environments. I've created a temperature.py file and saved it in a folder called python in my root directory: /Users/name
When I type python on the terminal and then run import temperature.py from the terminal I get this error:
ImportError: No module named 'temperature'
Where should I have saved the temperature.py file?
回答1:
The first place Python looks for modules to import is the working directory (i.e. the the directory of if you passed a script to python) or if you just launched python without a script, the directory you were in when you opened python. Failing to find it there, it uses the PYTHONPATH variable and if not found there either, it uses the a path specified in the installation of Python.
At runtime you can check with sys.path the actual paths it is looking in.
import sys
print(sys.path)
And you can even modify sys.path
if you need to. Add to the beginning as that is the place import will look first:
import sys
sys.path.insert(0, <path_of_temperature.py>)
Source https://docs.python.org/3/tutorial/modules.html
6.1.2. The Module Search Path
When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:
- The directory containing the input script (or the current directory when no file is specified).
- PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
- The installation-dependent default.
回答2:
Copy/move temperature.py
into your current working directory.
You can find out this directory from the Python prompt:
>>> import os
>>> os.getcwd()
回答3:
The temperature.py
file needs to be findable by Python. Python looks for importable packages in sys.path
:
>>> import sys
>>> print(sys.path)
['', ...]
You can either:
(1) add the temperature.py
file to a directory that is on sys.path
(the first item is the empty string so your current working directory will always work).
(2) dynamically add the directory (it's customary to add at the front)
import sys
sys.path.insert(0, path-to-directory-containing-temperature.py)
(3) Add the directory to the PYTHONPATH
environment variable.
(4) Create a package and install it (in development mode if you're working on it):
(dev) go|c:\srv\tmp\temp> cat temperature.py
def get_temp():
print 42
add a very minimalistic setup.py file:
(dev) go|c:\srv\tmp\temp> cat setup.py
from setuptools import setup
setup(
name='temp',
py_modules=['temperature']
)
install in dev mode
c:\srv\tmp\temp> python setup.py develop
running develop
running egg_info
...
Creating c:\python27\lib\site-packages\temp.egg-link (link to .)
Adding temp 0.0.0 to easy-install.pth file
Installed c:\srv\tmp\temp
Processing dependencies for temp==0.0.0
Finished processing dependencies for temp==0.0.0
now you can import it from anywhere (note that I'm starting from an entirely different directory):
c:\> python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import temperature
>>> temperature.get_temp()
42
When you're just starting out I would choose (1) or (3), after a while (4) will be the best option..
来源:https://stackoverflow.com/questions/41201748/importerror-no-module-named-temperature-from-the-terminal