Python Forum
running script from CLI in windows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
running script from CLI in windows
#1
hello,

I wish to run a python script, passing arguments, from command line (powershell core) without the need to write before the script name python.

I am from windows 10 and I am using anaconda.
I am aware that i can convert the .py to .exe but i am not willing because i need to make changes to it.
the script is in my path.

first from CMD I associated python script to my python.exe (i followed this https://stackoverflow.com/questions/1147...hon-in-cmd):

C:\Users\vincenzo>assoc .py
.py=Python
C:\Users\vincenzo>ftype Python
Python="C:\Users\vincenzo\Miniconda3\python.exe" "%1" %*
so now i try to run my script (wrap.py) from powershell, but it argue that i missed a package:

(base) PS C:\Users\vincenzo\Cloud\Hobby\Programmi\bin> ls


    Directory: C:\Users\vincenzo\Cloud\Hobby\Programmi\bin

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          06/03/2020    19:39       16116736 arduino-cli.exe
-a---          10/04/2019    19:13       66999808 ffmpeg.exe
-a---          05/02/2020    16:25       14153454 render_pcb.exe
-a---          06/04/2020    01:01             28 test.py
-a---          06/04/2020    01:29            850 wrap.py

(base) PS C:\Users\vincenzo\Cloud\Hobby\Programmi\bin> wrap
Traceback (most recent call last):
  File "C:\Users\vincenzo/Cloud/Hobby/Programmi/bin\wrap.py", line 6, in <module>
    import pyperclip, click
ImportError: No module named pyperclip
(base) PS C:\Users\vincenzo\Cloud\Hobby\Programmi\bin> wrap.py
Traceback (most recent call last):
  File "C:\Users\vincenzo/Cloud/Hobby/Programmi/bin\wrap.py", line 6, in <module>
    import pyperclip, click
ImportError: No module named pyperclip
(base) PS C:\Users\vincenzo\Cloud\Hobby\Programmi\bin> .\wrap.py
Traceback (most recent call last):
  File "C:\Users\vincenzo\Cloud\Hobby\Programmi\bin\wrap.py", line 6, in <module>
    import pyperclip, click
ImportError: No module named pyperclip
as you can see none worked, but i am sure that i have the package:

(base) PS C:\Users\vincenzo\Cloud\Hobby\Programmi\bin> conda list | grep pyperclip

pyperclip                 1.7.0                      py_0    conda-forge
and i was right.

I try another way and finally it works:

(base) PS C:\Users\vincenzo\Cloud\Hobby\Programmi\bin> python wrap.py
Usage: / [OPTIONS] WORD
Try "/ --help" for help.
Can someone help me to make it working this way?
wrap this_is_an_argument
Reply
#2
Have you verified PATHEXT has .py in it?

$env:PATHEXT += ";.py"
Reply
#3
yes it has, i forgot to wrote about it:

$env:pathext
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.CPL
I really can't understand why if i run the script without writing "python" it doesn't find the installed modules, i am getting mad at this
Reply
#4
The only reason should be that the module search path is different between the invocations. You could run python the different ways and have each print their search path.

import sys
print('\n'.join(sys.path))
Are they identical? Wildly different?
Reply
#5
You are right. It calls the wrong python, how is this possible?

(base) PS C:\Users\vincenzo\Desktop> python
Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print('\n'.join(sys.path))

C:\Users\vincenzo\Miniconda3\python37.zip
C:\Users\vincenzo\Miniconda3\DLLs
C:\Users\vincenzo\Miniconda3\lib
C:\Users\vincenzo\Miniconda3
C:\Users\vincenzo\Miniconda3\lib\site-packages
C:\Users\vincenzo\Miniconda3\lib\site-packages\win32
C:\Users\vincenzo\Miniconda3\lib\site-packages\win32\lib
C:\Users\vincenzo\Miniconda3\lib\site-packages\Pythonwin
>>>
>>> ^Z

(base) PS C:\Users\vincenzo\Desktop> wrapp
C:\Users\vincenzo\Cloud\Hobby\Programmi\bin
C:\Users\vincenzo\.platformio\python27\python27.zip
C:\Users\vincenzo\.platformio\python27\DLLs
C:\Users\vincenzo\.platformio\python27\lib
C:\Users\vincenzo\.platformio\python27\lib\plat-win
C:\Users\vincenzo\.platformio\python27\lib\lib-tk
C:\Users\vincenzo\.platformio\python27
C:\Users\vincenzo\.platformio\python27\lib\site-packages
As you can see in the first post I made the association to link .py script to python3
I copy here again the output:
Output:
C:\Users\vincenzo>ftype Python Python="C:\Users\vincenzo\Miniconda3\python.exe" "%1" %*
And the path is correct:
Output:
C:\Users\vincenzo>C:\Users\vincenzo\Miniconda3\python.exe Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32 Warning: This Python interpreter is in a conda environment, but the environment has not been activated. Libraries may fail to load. To activate this environment please see https://conda.io/activation Type "help", "copyright", "credits" or "license" for more information. >>> import pyperclip >>> >>>
Reply
#6
You are making this difficult when also bring Miniconda3 into this.
When using Anaconda/Miniconda (base) should be active(as you have),
then python and pip will always point to this (base) Virtual environment main installation C:\Users\vincenzo\Miniconda3.
Not using python it can/will break out and use other version.

The problem is also if not using python and mange to set ftype to Miniconda,
then (base) may not be active,and get trouble like this:
Error:
Warning: This Python interpreter is in a conda environment, but the environment has not been activated. Libraries may fail to load. To activate this environment please see https://conda.io/activation
Maybe there is a solution,but i would keep simple as i have Miniconda and Anaconda installed.
When use them i have (base) active and use python and pip no direct call without.

There also FileTypesMan that make it easier to change this,
but the (base) Virtual environment can make this tricky not using python.

I have Python 3.7(Python.org) set as main version for python with and without.
I use cmder as i do not at all like Powershell/cmd.
E:\div_code
λ python ver1.py
C:\python37\python.exe

E:\div_code
λ ver1
C:\Python37\python.exe

E:\div_code
λ pip -V
pip 20.0.2 from c:\python37\lib\site-packages\pip (python 3.7)

E:\div_code
λ python -V
Python 3.7.3
ftype "C:\Python37\python.exe" "%1"
Reply
#7
Quote:Maybe there is a solution,but i would keep simple as i have Miniconda and Anaconda installed.
okkkk I think that I am going to follow this suggestion, it is too much headache to just save 5 letters
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No Internet connection when running a Python script basil_555 8 442 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 335 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Triggering a ps1 script in remote windows server via http python request jasveerjassi 1 316 Jan-26-2024, 07:02 PM
Last Post: deanhystad
  Help Running Python Script in Mac OS emojistickers 0 306 Nov-20-2023, 01:58 PM
Last Post: emojistickers
  Trying to make a board with turtle, nothing happens when running script Quascia 3 607 Nov-01-2023, 03:11 PM
Last Post: deanhystad
  Python script running under windows over nssm.exe JaroslavZ 0 671 May-12-2023, 09:22 AM
Last Post: JaroslavZ
  Running script with subprocess in another directory paul18fr 1 3,475 Jan-20-2023, 02:33 PM
Last Post: paul18fr
  How to compile a Python script for a Windows / Linux executable? netanelst 2 1,272 May-24-2022, 07:02 AM
Last Post: netanelst
  Setup Portable Python on Windows for script starts with double clicks? pstein 0 1,778 Feb-18-2022, 01:29 PM
Last Post: pstein
  batch file for running python scipt in Windows shell MaartenRo 2 1,824 Jan-21-2022, 02:36 PM
Last Post: MaartenRo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020