Python from flash drive, how to start IDLE?

I tried to run Python 3.3 from my flash drive. I already tried Portable Python, but most of it did not open, and it crashed my laptop.

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

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

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

While I have it. It also asks for the drive letter as it 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 

He outputs this: IDLE Bringer Output

If you continue to follow the instructions and enter what it says, it calls IDLE. I could not figure out how to make the batch file actually enter into the Python request, so I told him to tell the user to enter what he had to type.

I need to know how I can change the PATH variable from a batch file. Also, how do I remove it when I am done (this is not so important and may even be in a separate batch file).

Or, alternatively, is there a way only for quick access to IDLE?

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

Thanks!

+4
source share
2 answers

You can invoke Python using this command line:

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

and it will invoke the IDLE shell without having to type anything.

Edit:. By the way, you really want to change the global path settings. Try to see if the following script can run Python the way you want. You must put it in the root of the USB drive where you have Python installed.

 @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()" 

Edit: Each process has an associated environment, which is a set of name-value pairs called environment variables. When the process starts, it receives a copy of the environment of its parent process. Global OS settings for environment variables are used for processes launched directly from the OS shell (GUI or command line). The set command in batch files sets or modifies an environment variable in the environment of the current process (not globally).

All set commands that you see in the above script only change the environment of the current process. These changes will be covered by the process created by the last line ( python.exe ), as it is a child of the shell process ( cmd.exe ) that executes the batch file.

Line

 set PATH=%PYTHON_HOME%;%PATH% 

Adds the contents of the PYTHON_HOME variable to the PATH variable of the current process. For example, if PATH was c:\foo\bar;d:\aaa\bbb and PYTHON_HOME were c:\python , then the new PATH value would be c:\python;c:\foo\bar;d:\aaa\bbb

+3
source

It is impossible to guarantee that this is possible if you do not have enough system privileges that you can change the global path. Actually, on most computers that you don’t have, they don’t actually, and I think this is the main goal. In cases where you have enough privileges (worth a try, some systems still allow this for ordinary users, but many others do not), you can use:

 setx PATH "%path%;yourpath" 

edit and ps:

You can identify the drive letter without input if you know the drive label, something like this:

 @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 

the idle running inside the package inherits the path, but other instances will not. It’s hard to verify completely tough.

An explanation of the script package above. The wmic command is not suitable for the windows command line command. One of them can use WMI to make WQL (SQL for WMI), as if the windows were a database. Databases contain many tables, in which case the computer is instructed to obtain a table called logicaldisk . The logicaldisk table has 38 columns and one row for each disk connected to the system. This is the way to a lot of data for this purpose. Thus, the data is filtered. WHERE forces the database to only spit out rows that contain a specific value, in which case it is only interested in rows in which the volumename columns are equal to DRIVENAME, you can also use the size of the serial number or any other criteria. Finally, GET is used to limit the columns that you return as results, since you are only interested in the name of the drive letter that you are asking. This is called the heading in the table, so you ask.

Since the command is a little long, so I put the command inside the variable (and not the result), this shortens the for line to fit the stack overflow. Since = needs to be escaped, I need to use the escape sequence ^ once again so that it is still capable in the for loop.

The for loop is used to capture the return value of the wmic command. Since the answer has many lines, I only filter lines containing the colon character. And put it in the pydrive variable.

0
source

Source: https://habr.com/ru/post/1498792/


All Articles