Unable to run azure command line - python

I have successfully installed the Azure Command line. Also, python is installed on the server. However, when I run /usr/bin/az command it gives error as follows:
Could not import runpy module
The output for python version is as follows:
Python 2.7.5 (default, Nov 16 2018, 04:38:38)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

The issue you are facing generally comes into the picture whenever there's a broken python environment on the host. In most cases, the fix is to re-install the package, but for the Azure CLI, you will have to make some other arrangements. I solved the issue by using the following method:
Step 1: Find the location of your AZ using which az most likely it should be sitting in /usr/bin/.
Step 2: Take a copy of your existing az and copy it to let's say az_copy
Step 3: Edit the az file using any of the available editors and replace with the following:
#!/usr/bin/env bash
#/usr/lib64/az/bin/python -Esm azure.cli "$#"
export LD_LIBRARY_PATH=/opt/rh/python27/root/usr/lib64
export PYTHONPATH=/usr/lib64/az/lib/python2.7:/usr/lib64/az/lib/python2.7/site-packages
/opt/rh/python27/root/usr/bin/python2.7 -sm azure.cli "$#"
# -E : ignore PYTHON* environment variables (such as PYTHONPATH)
# -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE
Post this, save and execute az, it should work.
Do let me know if that worked out for you.

Related

Git-Bash is Interpolating Environment Variables

I'm wondering if someone can explain the following strange behavior:
I have a python script using os.environ to pull environment variables for use with pysftp. The variable in question has been called "sftp_wd" and "sftp_wdir" with a value "/etc/<rest of path>" and the effect is the same for both.
>>> import os
>>> os.environ["sftp_wdir"]
This python code in cmd yields the correct value: /etc/...
But when run in git bash the value looks like: <%USERPROFILE%/AppData/Local/Programs/Git>/etc/...
I'm using the same conda environment in both cases and the effect shows up when run in the vscode debugger as well. Additionally if running python with winpty I don't see this effect. I've looked at the documentation on os.environ and for environment variables used by git bash and not found anything to explain this.
printenv also displays the correct value so I'm inclined to believe this is specific to how git bash is initializing python but I can't seem to pin down what's actually happening
Edit: In response to #CharlesDuffy it seems this can be replicated with any environment variable and any value starting with "/"
example I created an environment variable called random with a value of /var/. Results are as follows:
echo
$ echo $random
/var/
printf
$ printf '%q\n' "$random"
/var/
python
$ python
Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)] :: Ana
conda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['random']
'C:/Program Files/Git/var/'
>>>
Note: The change in the path to the "Git" folder from above is from updating to v2.33.0.2-64bit of Git Bash. The issue is still the same though.
This is behavior of msys, the Windows-compatibility library that Git Bash is compiled with. The documentation covering this behavior is at https://www.msys2.org/docs/filesystem-paths/
An environment variable is available to turn it off:
To turn off automatic path conversion in environment variables entirely, export MSYS2_ENV_CONV_EXCL='*'
To turn it off only for the variable named random: export MSYS2_ENV_CONV_EXCL=random

Python virtual environment does not default python packages

I have to create virtual environment and move it to some remote server.
For this i created environment but it does not contains default python packages like json and logging.
I am getting error when importing some package:
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'json'
As you can see json is coming from default path not from virtual environment
source venv3/bin/activate
(venv3) [user1#nn1 ~]$ python
Python 3.7.4 (default, Dec 12 2019, 12:03:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> print(json.__file__)
/usr/local/lib/python3.7/json/__init__.py
>>> import influxdb
>>> print(influxdb.__file__)
/home/user1/venv3/lib/python3.7/site-packages/influxdb/__init__.py
Virtualenvs are not movable between servers. Did you move the virtualenv to a new server? If so I think the easiest for you is to recreate it on the remote server.
Virtual environments use absolute paths (retrieved from environment variables, for example); you can find more information in the documentation or PEP 405. So even if you move your venv to another directory, it's most likely not going to work anymore. Venvs are not supposed to be moved around. Instead you have to recreate your venv wherever you copied your project.
Recreating your venv can be greatly simplified if you use libraries like poetry or pipenv, or simply a requirements.txt file.
Virtualenv does have the default package. If it doesn't have that then you would get the error on the import statement itself. Not on the further commands.

Why can't Anaconda python in shebang line find script (cygwin)?

I use cygwin on Windows 10, and want to write Python code
that uses Pandas and other packages contained in my Anaconda base
environment. I can run Python code if I give the absolute
path to the code, but not if bash needs to use PATH
to find the code.
I use these lines in my .bash_profile as suggested
here (search for 'bash_profile'):
. /opt/conda/etc/profile.d/conda.sh
conda activate base
Here's the start of my PATH, showing Anaconda stuff at the front:
$ echo ${PATH:1:160}
cygdrive/c/Users/Glenn/Anaconda3:/cygdrive/c/Users/Glenn/Anaconda3/Library/mingw-w64/bin:/cygdrive/c/Users/Glenn/Anaconda3/Library/usr/bin:/cygdrive/c/Users/Gle...
I can run Anaconda Python at the bash prompt:
$ which python
/cygdrive/c/Users/Glenn/Anaconda3/python
$ python -i
Python 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 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.
>>>
I can even run a Python program that uses Pandas:
$ pwd
/home/Glenn/bin
$ cat test.py
#!/usr/bin/env python
import pandas as pd
print(pd.Series([1,2]))
$ ./test.py
0 1
1 2
dtype: int64
The problem is that I can't run the program if I rely on the path:
$ which test.py
/home/Glenn/bin/test.py
$ test.py
C:\Users\Glenn\Anaconda3\python.exe: can't open file '/home/Glenn/bin/test.py': [Errno 2] No such file or directory
$
I get the same kind of error message if I invoke test.py from
another directory using an absolute pathname, but not if I invoke
it using a relative pathname.
I'm a longtime Cygwin user and haven't seen this kind of behavior
before, so suspect it has to do with Anaconda.
I can change to any other directory and get the same behavior.
Also, if I put the absolute path to Anaconda's Python in the
shebang line, I get the same behavior.
I've found (and tried) many suggestions for configuring Anaconda in a Cygwin environment, but many of them are 4 or 5 years old.
UPDATE: I was not able to solve this problem, but have switched to the Linux Windows Subsystem and no longer have to deal with the issue.

"python3" command not doing anything

When I run the python3 command, nothing happens. No stdout, no stderr... nothing. When I try the python2 command, however- I get the expected result. What is going on?
ubuntu#ip-172-91-23-255:~$ python3
ubuntu#ip-172-91-23-255:~$
ubuntu#ip-172-91-23-255:~$ which python3
usr/bin/python3
ubuntu#ip-172-91-23-255:~$ python2
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Just solved the same issue
If you renamed the python executable change it back, else, ignore this.
add the folder of python3 instalation to the PATH,
the instalation folder is possibly
this: C:\Users\RV420\AppData\Local\Programs\Python\Python37-32
To add folders to your path you can just search 'environment
variables' on windows, then click on environment variables and find
one called path
Now make sure that the new folder comes first in the path that the Python2 instalation folder
You're good to go

Vim with both python/dyn and python3/dyn uses python 2 by default

As you already know I have vim problems.
I usually use python 3 with pymode enabled.
Today I wanted to add a nice autocompletion for python to my vim and the things became messy.
I wanted to install [YouCompleteMe][1] with pathogen, and I saw that requires vim to be installed with python2 support.But pymode needs python3 support (or at least I need it with python3 support)
After a lot of googling I precompiled and installed both python versions with --enable-shared configuration and vim with both supports(python/dyn and python3/dyn)
The problem is that vim doesn't load python3 when needed.
I'm working on a project in python 3 and I tried adding this to the end of file:
import site
print(site.getsitepackages())
The output is:
['/usr/lib/python2.7/site-packages', '/usr/lib/site-python']
And if i type this as vim's command :
:py3 import site; print(site.getsitepackages())
The output is:
['/usr/lib/python3.4/site-packages', '/usr/lib/site-python']
I still haven't try to install YouCompleteMe...
My question is: how can I tell vim to use python3 by default instead of python2 ?
(without recomiling it with only python3 support)
Vim and its plugins are not pre-compiled objects files but are rather callable scripts which often simplify by not specifying version to be used.
If
$ python
Python 2.7.3 (default, Feb 27 2014, 19:58:35)
then vim is using python 2.7.
Changing default python is not a good idea. As i mentioned in my comment .. u should look at temporary option. One of the simplest option (bash-shell):
$ alias python='python3.2'
$ python
Python 3.2.3 (default, Feb 21 2014, 00:48:19)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Categories

Resources