Seite - 118 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 118 -
Text der Seite - 118 -
118 5 SomeMorePythonEssentials
WheretoPlacea ModuleFile?
For a module import to be successful, a first requirement is that Python can
find themodulefile.Asimplewaytomakethishappen, is toplacethemodule
filein thesamefolderastheprogram(that tries to import themodule).a There
areotheralternatives,but thenyoushouldknowhowPythonlooksformodule
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,wemaydo:
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’]
Placingyourmodule inoneof the folders listed, assures thatPythonwill find
it. If, for some reason, you want to place your module in a folder that is not
listed insys.path, you may insert that folder name insys.path. With our
sys.pathhere,wecould insert thefoldernamemy_folder, forexample,by
continuingour sessionas
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 toinsertgives the location in thesys.path list where
you want the folder name to be inserted (0 gives first place, 1 gives second,
andso on).
You may also use the PYTHONPATH variable (https://docs.python.org/3/
using/cmdline.html#envvar-PYTHONPATH),but theaboveshouldsufficefor
abeginner.
Ifyouwanttorunyourmodulefilealsoasaprogram,thelocationofthefile
might require that you first update the PATH environmentvariable (see, e.g.,
http://hplgit.github.io/primer.html/doc/pub/input/._input-readable009.html).
a In thisbook, we keep to this simple way.***
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik