118
5 Some More Python Essentials
Where to Place a Module File?
For a module import to be successful, a first requirement is that Python can
find the module file. A simple way to make this happen, is to place the module
file in the same folder as the program (that tries to import the module). a There
are other alternatives, but then you should know how Python looks for module
files.
When Python proceeds to import a module, it looks for the module file
within the folders contained in the sys.path list. To see the folders in
sys.path, we may do:
In [1]: import sys
In [2]: sys.path
Out[2]:
[’’,
’/.../.../site-packages/spyder/utils/site’,
...
...
< longer printout... author’s comment >
...
’/.../.../site-packages/IPython/extensions’,
’/.../.../.ipython’]
Placing your module in one of the folders listed, assures that Python will find
it. If, for some reason, you want to place your module in a folder that is not
listed in sys.path, you may insert that folder name in sys.path. With our
sys.path here, we could insert the folder name my_folder, for example, by
continuing our session as
In [3]: sys.path.insert(0, ’my_folder’) # 0 - location in sys.path
In [2]: sys.path
Out[2]:
[’my_folder’,
’’,
’/.../.../site-packages/spyder/utils/site’,
...
...
...
’/.../.../site-packages/IPython/extensions’,
’/.../.../.ipython’]
The first argument to insert gives the location in the sys.path list where
you want the folder name to be inserted (0 gives first place, 1 gives second,
and so on).
You may also use the PYTHONPATH variable (https://docs.python.org/3/
using/cmdline.html#envvar-PYTHONPATH), but the above should suffice for
a beginner.
If you want to run your module file also as a program, the location of the file
might require that you first update the PATH environment variable (see, e.g.,
http://hplgit.github.io/primer.html/doc/pub/input/._input-readable009.html).
a In this book, we keep to this simple way.***
5 Some More Python Essentials
Where to Place a Module File?
For a module import to be successful, a first requirement is that Python can
find the module file. A simple way to make this happen, is to place the module
file in the same folder as the program (that tries to import the module). a There
are other alternatives, but then you should know how Python looks for module
files.
When Python proceeds to import a module, it looks for the module file
within the folders contained in the sys.path list. To see the folders in
sys.path, we may do:
In [1]: import sys
In [2]: sys.path
Out[2]:
[’’,
’/.../.../site-packages/spyder/utils/site’,
...
...
< longer printout... author’s comment >
...
’/.../.../site-packages/IPython/extensions’,
’/.../.../.ipython’]
Placing your module in one of the folders listed, assures that Python will find
it. If, for some reason, you want to place your module in a folder that is not
listed in sys.path, you may insert that folder name in sys.path. With our
sys.path here, we could insert the folder name my_folder, for example, by
continuing our session as
In [3]: sys.path.insert(0, ’my_folder’) # 0 - location in sys.path
In [2]: sys.path
Out[2]:
[’my_folder’,
’’,
’/.../.../site-packages/spyder/utils/site’,
...
...
...
’/.../.../site-packages/IPython/extensions’,
’/.../.../.ipython’]
The first argument to insert gives the location in the sys.path list where
you want the folder name to be inserted (0 gives first place, 1 gives second,
and so on).
You may also use the PYTHONPATH variable (https://docs.python.org/3/
using/cmdline.html#envvar-PYTHONPATH), but the above should suffice for
a beginner.
If you want to run your module file also as a program, the location of the file
might require that you first update the PATH environment variable (see, e.g.,
http://hplgit.github.io/primer.html/doc/pub/input/._input-readable009.html).
a In this book, we keep to this simple way.***
