Installing pythonstartup file

帅比萌擦擦* 提交于 2019-12-17 10:58:49

问题


How do I install the pythonstartup file so that it runs on command like python myfile.py?

I tried to install it into my /home/myuser directory for Ubuntu, but it said that I had insufficient permissions. Furthermore, different places say alternately that it should be in all caps or in all lower case with a period before it. The Python command line guide does not seem to explain where to put the file, or how to change which environment variable to 'point' to it.


回答1:


In your ~/.bashrc:

export PYTHONSTARTUP=$HOME/.pythonstartup

and put your python code in $HOME/.pythonstartup, like:

import rlcompleter
import readline

readline.parse_and_bind("tab: complete")

Then run the interactive shell:

python

See the imports from PYTHONSTARTUP are processed. This only works in python interactive mode.

For more information about PYTHONSTARTUP variable, read the python man page:

$ man python



回答2:


On Windows you can put your startup script just about anywhere as long as you put its path into your PYTHONSTARTUP environment variable. On Windows the lettercase of environment variable names doesn't matter.

You can define the values of user and system environment variables as described in a somewhat related answer of mine here.




回答3:


I'm assuming you mean the PYTHONSTARTUP environment variable? Try putting a file my-python-startup.py with some interesting contents in your home dir, then issue the following on the command line:

export PYTHONSTARTUP=$HOME/my-python-startup.py
python

and observe what happens. Then put the first of the above lines in the (hidden!) file .bashrc in your homedir to have it persist across terminal sessions.




回答4:


How to execute the python file defined in $PYTHONSTARTUP when you execute a file like python foobar.py

Run this command to find out where your OS has defined USER_SITE:

$ python -c "import site; site._script()" 

Mine says:

USER_SITE: '/home/el/.local/lib64/python2.7/site-packages'

Create a new file there called /home/el/.local/lib64/python2.7/site-packages/usercustomize.py, put this code in there:

try:
    import your_things
    import readline
    print("ROCKETMAN!")

except ImportError:
    print("Can't load your things")
    print("Either exit here, or perform remedial actions")
    exit()

Close the terminal and reopen it to clear out any shenanigans.

Make a new file python foobar.py anywhere on the filesystem, put this code in there:

#Note there is no your_things module imported here.
#Print your_things version:
print(your_things.__version__)

Run it:

python foobar.py 
ROCKETMAN!
'1.12.0'

What just happened.

You used the python sitewide specific python configuration hook and imported libraries in the usercustomize.py file which ran before foobar.py.

Documentation: https://docs.python.org/2/library/site.html

Where I found this trick: https://nedbatchelder.com/blog/201001/running_code_at_python_startup.html



来源:https://stackoverflow.com/questions/5837259/installing-pythonstartup-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!