4

I was trying to run Python 3.3 off of my flash drive. I already tried Portable Python, but most of what it had wouldn't open, and it crashed my laptop.

So, I deleted it, and installed regular Python. Then, I wanted to start adding my favorite modules. And, I needed a way to start IDLE without the original shortcut.

To install modules, I added my Python install to my PATH variable.

To make all this easier, I made a batch file, using some code I found on this question.

So far, I have this. It also asks for the drive letter, because that changes from computer to computer.

@echo off
echo This only works if your Python install is added to the system-wide PATH variable
set /p Path="Enter the Drive Letter on this computer. No Symbols, Just the Letter, Capital"
cd %Path%:\Program Files\Python33
echo type this when python comes up...
echo import idlelib.PyShell
echo idlelib.PyShell.main()
echo.
echo.
echo.
echo.
python

It outputs this: IDLE Bringer Output

If you go on and follow the instructions and type what it says, it brings up IDLE. I couldn't figure out how to get the batch file to actually type into the Python prompt, so I told it to tell the user to type what needed to be typed.

What I need to know is, how can I change the PATH variable from within the batch file. Also, how to I remove it when I'm done (this isn't as important, and could even be in a separate batch file).

Or, alternatively, is there a way just to shortcut to IDLE?

Also, is there a way to run .py files without the command line, with the Python install on my flash drive?

Thanks!

4

2 回答 2

3

您可以使用以下命令行调用 Python:

python -c"import idlelib.PyShell;idlelib.PyShell.main()"

它会在不需要用户输入任何内容的情况下启动 IDLE shell。

编辑:顺便说一句,你确定你真的需要更改全局路径设置。试试看下面的脚本能否按照你想要的方式启动 Python。您必须将它放在安装 Python 的 USB 驱动器的根目录中。

@echo off
setlocal
set SCRIPT_DIR=%~dp0
:: Removes trailing backslash (for readability in the following)
set SCRIPT_DIR=%SCRIPT_DIR:~0,-1%
set PYTHON_HOME=%SCRIPT_DIR%\Program Files\Python33
set PATH=%PYTHON_HOME%;%PATH%
"%PYTHON_HOME%\python.exe" -c"import idlelib.PyShell;idlelib.PyShell.main()"

编辑:每个进程都有一个关联的environment,它是一组名为environment variables的名称-值对。当一个进程启动时,它会获得其父进程的环境副本。环境变量的全局 OS 设置用于直接从 OS(GUI 或命令行)shell 启动的进程。批处理文件中的set命令设置或修改当前进程环境中的环境变量(不是全局的)。

set您在上述脚本中看到的所有命令仅更改当前进程的环境。最后一行 ( ) 创建的进程将看到这些更改,python.exe因为它是执行批处理文件的命令 shell ( ) 进程的子进程。cmd.exe

线

set PATH=%PYTHON_HOME%;%PATH%

PYTHON_HOME将变量的内容添加到PATH当前进程的变量中。例如,如果PATHwasc:\foo\bar;d:\aaa\bbbPYTHON_HOMEwere那么willc:\python的新值是PATHc:\python;c:\foo\bar;d:\aaa\bbb

于 2013-08-25T11:04:44.037 回答
0

除非您有足够高的系统权限可以更改全局路径,否则无法保证这是可能的。在您不拥有的大多数计算机上,确实没有办法解决这个问题,我想这是主要目的。在这些情况下,当您拥有足够的权限时(值得一试,一些系统仍然允许普通用户使用,但许多其他系统不允许),您可以使用:

setx PATH "%path%;yourpath"

编辑和ps:

如果您知道磁盘标签,则无需输入即可找出驱动器号,如下所示:

@echo off
set label=DRIVENAME
set cmd=WMIC logicaldisk WHERE volumename^^="%label%" GET caption


FOR /F "tokens=1" %%G IN ('%cmd% ^| find ":"')DO set pydrive=%%G

echo %pydrive%\pathtopython
rem do your stuff here

批处理中开始的空闲将继承路径,但其他实例不会。很难最终测试。

上面的批处理脚本的解释。命令wmic是 windows 管理工具命令行的缩写。可以使用 WMI 做很多事情,其中​​之一是发出 WQL(SQL for WMI)查询,就好像 windows 是一个数据库一样。数据库包含许多表,在这种情况下,计算机被指示获取名为logicaldisk的表。表logicaldisk对于​​连接到系统的每个磁盘有38 列和1 行。为此目的,这是获取大量数据的方式。所以数据被过滤了。WHERE导致数据库仅吐出包含某些特定值的行,在这种情况下,它只对列 volumename 等于 DRIVENAME 的行感兴趣,同样您可以使用序列号大小或任何其他标准。最后GET用于限制您作为结果返回的列,因为您只对您所要求的驱动器的字母名称感兴趣。这被称为表中的标题,以便您问什么。

由于命令有点长,所以我将命令放在一个变量中(不是结果),这缩短了 for 行,因此它适合堆栈溢出。因为 = 需要转义,所以我需要多次使用转义序列 ^ 所以它仍然可以在 for 循环中使用。

for 循环用于捕获 wmic 命令的返回值。由于答案有很多行,我只过滤包含冒号字符的行。并将其放入变量pydrive。

于 2013-08-25T06:54:39.960 回答