Why can't I run a python command via a powershell script? - azure

I have a powershell script which is meant to create a virtual environment with the following command:
py -3.6 -m venv --clear env
I have verified that I can do this when I run the above command in a powershell window, and the environment is created. However when I run the powershell script via an azure release pipeline, I get the following error:
python : The term 'python' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
I have verified that my user PATH environment variable has all the necessary paths:
C:\Python36\Scripts\;C:\Python36\;%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;C:\Python36\Lib\site-packages\pywin32_system32;C:\Python36\Lib\site-packages\win32;C:\Automation\app\env\Scripts;C:\Python38;C:\Python38\Scripts;C:\Python38\Lib\site-packages\win32;C:\Python38\Lib\site-packages\pywin32_system32
Specs:
Python: 3.6.7
OS: Windows 10
Environment: Azure virtual machine.
Any help would be much appreciated.

There seems to be a change when the variable is injected into the pipeline.
Try using:
PYTHON -3_6 -M VENV --clear ENV
Environment variables:
Environment variables are specific to the operating system you are using. They are injected into a pipeline in platform-specific ways. The format corresponds to how environment variables get formatted for your specific scripting platform.
On UNIX systems (macOS and Linux), environment variables have the format $NAME. On Windows, the format is %NAME% for batch and $env:NAME in PowerShell.
System and user-defined variables also get injected as environment variables for your platform. When variables are turned into environment variables, variable names become uppercase, and periods turn into underscores. For example, the variable name any.variable becomes the variable name $ANY_VARIABLE.
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#environment-variables

It runs out I hadn't added the paths to the system variables (in windows there are user environment variables and system environment variables, and pywin32 uses the system ones.

Related

Why does cmd and powershell recognize "python" but not "py"?

I seem to be having the opposite issue as this user in this question. I can run python successfully, but py does not work for me. Overall, this is not a big deal but when I try to run code in VS Code, I get the error below:
py: The term 'py' is not recognized as a name of a cmdlet, function, script file, or executable program.
How can I either make VScode run python or change my system to accept py?
When I first downloaded Python, it was not placed into the path but I was able to fix that. I also confirmed that VS Code has selected the correct interpreter for Python in the same place.
EDIT: IMAGES
Path directory
Path used in enviroment
When you write "python" on CMD or Powershell, it checks the given keyword("python" here) in all the directories in Environment Variables.
In the case of python, the directory mentioned for python in Environment Variables > Path, you will find "python.exe" and not "py.exe".
So your cmd or PowerShell recognizes python and not py.

How to set environment path to folder in my documents directory on mac

Hello I am working on a hyper ledger fabric project using python
I am running into an error on my program which requires me to set an environment path
below is the error given :
configtxgen not in PATH.
Configtx is empty
now I know the fix for this, it requires me setting an environment path to a file called
configtx.yaml
its location on my Mac is
documents/blockchain/fabric-sdk-py/test/fixtures/e2e_cli/configtx.yaml
please how do I set an environment path towards this file
Thanks
How you set an env var depends on your shell and whether you are running the program from a shell prompt or some other way (e.g., double-clicking on an application icon). Assuming you're using a POSIX shell like bash or zsh you would type something like this:
export VAR_NAME="$HOME/documents/blockchain/fabric-sdk-py/test/fixtures/e2e_cli/configtx.yaml"
You would normally put that statement in a file like ~/.bashrc so it is set each time you start an interactive shell.
P.S., If the documentation for the application you're using doesn't tell you how to do this I would open an issue suggesting such instructions be added.

Issue tying to read environment variable in Python

Hi I'm trying to read an environment variable in python, if I execute my script from console with:
python3 myScript.py everything goes ok, but I need to run this script as a service in Ubuntu, in this case, the script can't get the environment variable. Anyone has past for the same issue? I noticed that when i'm trying to read the mongo URI from environment.
The correct way to achieve that is passing the environment variable or environment file to the service file, in my case I added this line to my service file:
EnvironmentFile = /etc/environment

How to run virtual environment from shell script?

I am trying to setup my project environment from a shell script on ubuntu so that I can skip basic setup every time.
I use my virtual environmet cv using the command workon cv.
But I am having trouble doing so using a shell script. I tried the the script
#!/bin/bash
workon cv
But I get the error
workon: command not found
I try to list all the venv I have
pran#pran-HP-65-Notebook-PC:~$ lsvirtualenv
cv
==
virtual-py2
===========
Also, I thought of locating it
(cv) pran#pran-HP-65-Notebook-PC:~/.virtualenvs$ l
cv/ postdeactivate preactivate* prermvirtualenv*
get_env_details* postmkproject* predeactivate virtual-py2/
initialize postmkvirtualenv premkproject*
postactivate postrmvirtualenv* premkvirtualenv*
How can I do it?
You cannot execute workon command outside of Python virtual environment.
If you are interested, check out this article that will walk you through all of the steps required to set up your virutal env.
If it is not what you need then please clarify your requirements for a project environment.
I found the solution:
My venv was located in .virtualenvs. So, I put the command in the bash file start.sh (meant to be executed before working on my project).
#start.sh
source ~/.virtualenvs/cv/bin/activate
And run it using
$ source start.sh
It works perfectly 👍

Difference between $PATH, sys.path and os.environ

What is the difference between $PATH variable, sys.path and os.environ? I understand that they both serve as paths where python searches packages. But it'd be nice to have more elaborate response.
Just a working case from my practice is when I used the script with only os.environ before import on Ubuntu 16.04 I got ImportError: No module named XXX. At the same time on MacOS it worked well. After I added sys.path on Ubuntu I could get import module well.
This is actually more complicated than it would seem. It's unclear by the question if you understand the Linux/MacOS $PATH environment variable. Lets start there. The $PATH variable (in Python you're able to access the system environement variables from os.environ) denotes the current users $PATH variable as defined in various shell profile and environment files. It typically contains things like "/usr/bin" and other places where programs are installed. For example when you type "ls" into the system shell, the underlying system searches the $PATH for programs named "ls". So what actually gets executed is probably something like "/usr/bin/ls" I've included additional reading below.
sys.path on the other hand is constructed by Python when the interpreter is started, based on a number of things. The first sentence in the help page is as follows. "A list of strings that specifies the search path for modules. Initialized from the environment variable $PYTHONPATH, plus an installation-dependent default." The installation-dependent portion typically defines the installation location of Python site packages. $PYTHONPATH is another environment variable (like $PATH) which can be added to facilitate the module search location and can be set the same way the system $PATH can
Typically if you have non-installed sources (ie you have Python files that you want to run outside the site-packages directory) you typically need to manipulate sys.path either directly in your scripts or add the location to the $PYTHONPATH environment variable so the interpreter knows where to find your modules. Alternatively, you could use .pth files to manipulate the module search path as well
This is just a basic overview, I hope you read the docs for better understanding
Sources
Linux $PATH variable information
Python sys.path
Python site.py
sys.path
Is a list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.
os.environ
Is a mapping object representing the string environment. For example, environ['HOME'] is the pathname of your home directory (on some platforms), and is equivalent to getenv("HOME") in C.
Environment variable PATH
Specifies a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting.

Resources