Click here to Skip to main content
15,885,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I want to run a script called scan.py in my Command terminal. I type in
python scan.py
. I got told, since this is not working, to open the CMD window as admin. So the window shows me C:\Windows\system32>.
So my question is, how do I run my script from there, without having to type in the path to the script.

What I have tried:

(I tried that and it would run as a normal user again, not as admin) Should I move the file in to the directory system32?
Running
Python
"runas /user:administrator C:\users....\scan.py
asks me for the password of the admin, but all of my suggestions were (I tried my own password and just hitting enter, both didn't work) wrong.
Posted
Updated 22-Feb-18 15:00pm

By default, when you open a command prompt as an administrator, the current directory is set to C:\Windows\System32.
You just have to navigate to the directory (cd path\to\your\py\file) where your .py file is located and issue the python scan.py command (assuming the python executable is in your path variable, which seems to be the case).

"Should I move the file in to the directory system32"
In no circumstance you should put a python script in this directory. Better understand the principle of paths and commands.

runas /user:administrator C:\users....\scan.py
You should understand that your have to launch the python executable, not the .py script directly.
Correct command would better be runas /user:administrator python "C:\users....\scan.py"
Better enclose the full path to the script between quotes, just to be sure it is understood as an argument to the python executable.
 
Share this answer
 
Comments
pycoding 23-Feb-18 12:43pm    
Thanks for your reply. Running "runas /user:administrator python "C\users\....\scan.py" tells me: RUNAS Syntax
RUNAS [ [/noprofile |/profile] [/env] [/savecred] [/netonly] ]
/smartcard [/user:<benutzername>] Programm

RUNAS [ [/noprofile |/profile] [/env] [/savecred] ]
/smartcard [/user:<benutzername>] Program
RUNAS /trustlevel: <trstlevel> program

/explantion of all [/xxxx]

examples:
>runas /noprofile /user:mymachine\administrator cmd
>two more exmaples ..

note: /profile is not compatible with /netonly
Note: /savecred is not compatible with /smartcard

I don't know what to do with all this, but I somehow have to put in something more into my cmd..?!
phil.o 23-Feb-18 13:36pm    
You could try
runas /user:administrator "python C:\users....\scan.py"
(enclosing the whole python... command between quotes)
Or you could also have a look at that SO post: Request UAC elevation from within a Python script?.
Quote:
How do I run commands/scripts as an admin?

First thing, in order to run an app as admin, you need to have admin rights or know an admin username and password.
Quote:
So my question is, how do I run my script from there, without having to type in the path to the script.

Short answer, you don't. It is like sending a mail without writing the address.
Quote:
Should I move the file in to the directory system32?

Bad idea, this directory is protected and is not for general usage. It is better to move to correct directory like:
CD c:\myapp
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900