Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
安装pythonstartup文件_Python_Unix_Windows 7_Installation - Fatal编程技术网

安装pythonstartup文件

安装pythonstartup文件,python,unix,windows-7,installation,Python,Unix,Windows 7,Installation,如何安装pythonstartup文件,使其在类似pythonmyfile.py的命令上运行 我试图将它安装到Ubuntu的/home/myuser目录中,但它说我没有足够的权限。此外,不同的地方交替地说,它应该在所有大写或小写字母中,前面有一个句号。Python命令行指南似乎没有解释将文件放在何处,或者如何将哪个环境变量“指向”它 在您的~/.bashrc中: export PYTHONSTARTUP=$HOME/.pythonstartup 并将python代码放入$HOME/.pytho

如何安装
pythonstartup
文件,使其在类似
pythonmyfile.py的命令上运行


我试图将它安装到Ubuntu的
/home/myuser
目录中,但它说我没有足够的权限。此外,不同的地方交替地说,它应该在所有大写或小写字母中,前面有一个句号。Python命令行指南似乎没有解释将文件放在何处,或者如何将哪个环境变量“指向”它

在您的
~/.bashrc
中:

export PYTHONSTARTUP=$HOME/.pythonstartup
并将python代码放入
$HOME/.pythonstartup
中,如:

import rlcompleter
import readline

readline.parse_and_bind("tab: complete")
然后运行交互式shell:

python
请参见处理从PYTHONSTARTUP导入的内容。这只适用于python交互模式

有关
PYTHONSTARTUP
变量的更多信息,请阅读python手册页:

$ man python
我假设你指的是环境变量?尝试在home dir中放置一个包含一些有趣内容的文件
my python startup.py
,然后在命令行上发出以下命令:

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

观察发生了什么。然后将上面的第一行放入homedir中的(隐藏!)文件
.bashrc
,使其在终端会话中保持不变。

在Windows上,只要将启动脚本的路径放入PYTHONSTARTUP环境变量中,就可以将其放在任何位置。在Windows上,环境变量名称的字母大小写并不重要

您可以定义用户和系统环境变量的值,如我的一个有点相关的回答中所述。

当您执行类似
python foobar.py的文件时,如何执行$PYTHONSTARTUP中定义的python文件
运行此命令以查找操作系统在何处定义了用户站点

$ python -c "import site; site._script()" 
USER_SITE: '/home/el/.local/lib64/python2.7/site-packages'
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()
#Note there is no your_things module imported here.
#Print your_things version:
print(your_things.__version__)
我的说:

$ python -c "import site; site._script()" 
USER_SITE: '/home/el/.local/lib64/python2.7/site-packages'
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()
#Note there is no your_things module imported here.
#Print your_things version:
print(your_things.__version__)
在那里创建一个名为
/home/el/.local/lib64/python2.7/site packages/usercustomize.py的新文件,将此代码放在那里:

$ python -c "import site; site._script()" 
USER_SITE: '/home/el/.local/lib64/python2.7/site-packages'
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()
#Note there is no your_things module imported here.
#Print your_things version:
print(your_things.__version__)
关闭航站楼并重新打开,以清除任何恶作剧。

在文件系统的任何位置创建一个新文件
python foobar.py
,将此代码放在那里:

$ python -c "import site; site._script()" 
USER_SITE: '/home/el/.local/lib64/python2.7/site-packages'
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()
#Note there is no your_things module imported here.
#Print your_things version:
print(your_things.__version__)
运行它:

python foobar.py 
ROCKETMAN!
'1.12.0'
刚刚发生的事。

您在foobar.py之前运行的
usercustomize.py
文件中使用了python站点范围内特定的python配置挂钩和导入的库

文件:


在那里我发现了这个技巧:

很棒的东西。谢谢你也可以试试bpython,它有这个功能和其他一些功能,而且更容易安装。太好了。在我的Windows 7机器上工作我有一个docker容器,entrypoint.sh在其中调用我不想修改的python文件。这起作用了;PYTHONSTARTUP没有。(我本可以添加
execfile(os.environ['PYTHONSTARTUP'])
,但在审查时会重新考虑。)