I have a folder structure for a Python project as follows:
proj/
├── cars
│ ├── honda.py
│ └── volvo.py
├── trucks
│ ├── chevy.py
│ └── ford.py
├── main.py
└── params.py
Contents of params.py
:
""" Parameters used by other files. """
serial = '12-411-7843'
Contents of honda.py
:
""" Information about Honda car. """
from params import serial
year = 1988
s = serial
print('year is', year)
print('serial is', s)
From within the proj/
folder I can run scripts using iPython:
$ cd path/to/proj/
$ ipython
In [1]: run cars/honda.py
year is 1988
serial is 12-411-7843
If I try to run the script using the python
command, I get a module not found error for params.py
:
$ cd path/to/proj/
$ python cars/honda.py
Traceback (most recent call last):
File "cars/honda.py", line 5, in <module>
from params import serial
ModuleNotFoundError: No module named 'params'
Why doesn't the approach using the python
command work?
NOTE - The examples above are executed on a Mac using the Anaconda Python distribution. There is a similar question about an import issue when running on Windows vs Linux machines. However, my question is related to using iPython vs python
on the Mac to run scripts.