0

I'm new to python and enjoying learning the language. I like using the interpreter in real time, but I still don't understand completely how it works. I would like to be able to define my environment with variables, imports, functions and all the rest then run the interpreter with those already prepared. When I run my files (using PyCharm, Python 3.6) they just execute and exit.

Is there some line to put in my .py files like a main function that will invoke the interpreter? Is there a way to run my .py files from the interpreter where I can continue to call functions and declare variables?

I understand this is a total newbie question, but please explain how to do this or why I'm completely not getting it.

Malkintosh
  • 13
  • 2
  • http://stackoverflow.com/questions/11124578/automatically-import-modules-when-entering-the-python-or-ipython-interpreter –  May 12 '17 at 17:37

3 Answers3

0

You can achieve the result you intend by doing this:

  1. Write a Python file with all the imports you want.
  2. Call your script as python -i myscript.py.

Calling with -i runs the script then drops you into the interpreter session with all of those imports, etc. already executed.

If you want to save yourself the effort of calling Python that way every time, add this to your .bashrc file:

alias python='python -i /Users/yourname/whatever/the/path/is/myscript.py'
Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
0

You set the environment variable PYTHONSTARTUP as suggested in this answer: https://stackoverflow.com/a/11124610/1781434

Community
  • 1
  • 1
0

I think you're asking three separate things, but we'll count it as one question since it's not obvious that they are different things:

1. Customize the interactive interpreter

I would like to be able to define my environment with variables, imports, functions and all the rest then run the interpreter with those already prepared.

To customize the environment of your interactive interpreter, define the environment variable PYTHONSTARTUP. How you do that depends on your OS. It should be set to the pathname of a file (use an absolute path), whose commands will be executed before you get your prompt. This answer (found by Tobias) shows you how. This is suitable if there is a fixed set of initializations you would always like to do.

2. Drop to the interactive prompt after running a script

When I run my files (using PyCharm, Python 3.6) they just execute and exit.

From the command line, you can execute a python script with python -i scriptname.py and you'll get an interactive prompt after the script is finished. Note that in this case, PYTHONSTARTUP is ignored: It is not a good idea for scripts to run in a customized environment without explicit action.

3. Call your scripts from the interpreter, or from another script.

Is there a way to run my .py files from the interpreter where I can continue to call functions and declare variables?

If you have a file myscript.py, you can type import myscript in the interactive Python prompt, or put the same in another script, and your script will be executed. Your environment will then have a new module, myscript. You could use the following variant to import your custom definitions on demand (assuming a file myconfig.py where Python can find it):

from myconfig import *

Again, this is not generally a good idea; your programs should explicitly declare all their dependencies by using specific imports at the top.

Community
  • 1
  • 1
alexis
  • 48,685
  • 16
  • 101
  • 161