Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of a PATH variable for IPython?

Tags:

python

ipython

Say I have a python script test.py in some path path_A

And say I have an IPython shell open in a path path_B.

I would like to be able to do:

run test.py

from path_B (where the shell is open).

Is that possible in IPython? Is there anything like a PATH variable in IPython?

like image 870
Amelio Vazquez-Reina Avatar asked Feb 28 '26 01:02

Amelio Vazquez-Reina


1 Answers

Not how you describe. The usual way is to os.chdir(path_A) within ipython first, or just run path_A/test.py as Thomas said in the comments.

Adding the PYTHONPATH environment variable, as suggested in another answer here, will not work for run because this is only used for searching for import modules.

An alternative, is to put path_A into sys.path (you can do so using PYTHONPATH environment variable, or preferably, in the ipython config file which runs at startup). Then you would be able to do:

import test
test.main()

This method would necessitate you to restructure your code in test.py, so that it ran at call time rather than at import time.

like image 105
wim Avatar answered Mar 01 '26 13:03

wim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!