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.
Python being the new fashionable language, especially in Finance industry, I started learning it.
I downloaded it from the Python website - Python version 3.5.2 - and it successfully installed... in my Application folder.
Just so you know a little bit more about myself, I did an IT engineering school in France, so I have an IT culture, but I never had the soul of a hacker so some things might be more difficult than others.
Started to code on Python IDE, then I created an executable python file and when I tried to execute it ... error!!
This is what I got when I executed my file (bissextile.py - file is supposed to ask the user to enter a year and tell him if this year is bissextile):
Last login: Tue Aug 9 23:24:02 on ttys000
MacBook-Pro-de-Tebah:~ tebahsaboun$ cd '/Users/tebahsaboun/Desktop/' && '/usr/bin/pythonw' -d -v '/Users/tebahsaboun/Desktop/bissextile.py' && echo Exit status: $? && exit 1
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py
import site # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py
import os # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc
import errno # builtin
import posix # builtin
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py
import posixpath # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.py
import stat # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.py
import genericpath # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/genericpath.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/warnings.pyc matches
[...] A LOT OF STUFF THAT'S THE SAME AS BEFORE
import encodings.aliases # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/aliases.pyc
# /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.pyc matches /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py
import encodings.utf_8 # precompiled from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.pyc
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
File "/Users/tebahsaboun/Desktop/bissextile.py", line 1
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
^
SyntaxError: unexpected character after line continuation character
# A lot of cleaning up files after that...
So, I saw here two issues:
1) my file doesn't work ...
2) the shell is not using Python 3.5 but Python 2.7 which is the problem I need help for.
Lots of research on internet, I was about to follow that tutorial:
https://wolfpaulus.com/journal/mac/installing_python_osx/comment-page-3/#comment-101285
...and about to delete some system files from my mac :) which it didn't let me do...decided that I didn't really know what I was doing and that I should dig a little more in the internet.
For the record, here is what I have in /System/Library/Frameworks/python.framework/Versions/ :
So I found something called "Homebrew" that is suppose to install Python for you and I followed this great tutorial:
http://blog.manbolo.com/2014/09/27/use-python-effectively-on-os-x#p1
Asking Homebrew to install Python 3.5 instead of Python 2.7 (steps are absolutely the same) which I apparently did.
So I did the verification suggested in the tutorial to be sure that I was using the right version of Python but when I asked the shell I still got Python 2.7.
I checked my /etc/paths file which is as follows:
/usr/local/bin
/usr/bin
/bin
/usr/bin
/bin
/usr/local/bin is the first binary in the file and I checked the folder it contains Python 3.5 indeed. But no matter what is the first line is in that file I get :
MacBook-Pro-de-Tebah:~ tebahsaboun$ which python
/usr/bin/python
and :
**MacBook-Pro-de-Tebah:~ tebahsaboun$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.**
I can't put all the links that I have researched so far ( especially stack overflow articles by the way ;) ) cause I don't have enough "reputation", but I did a lot of research and no article seems to respond to my problem. And I don't understand what is wrong.
Run the program from command line using python3 my_script.py or add shebang #!/urs/bin/python3
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.
I am confused in trying to understand which python version is used when I run them in interpreter?
That is, how the shell decides which python version to load when I have more than one versions.
I have snapshot of my /usr/bin. It has atleast two different python versions2.5 and 2.6.
I have not specified anything in my .bashrc to choose which one from.
when I execute python command on bash shell, python interpreter opens up, but loads with version 2.6.
How is this detected by shell?
If I want to change versions, how will I do that? That is, I want to change the default version
lastly what kind of files are these in /usr/bin/python. I tried to open them in editor and I see some ###. I have read many posts here but some makes me confusing.
ls /usr/bin/python
python python2.5 python2.6 pythonw pythonw2.6
python-config python2.5-config python2.6-config pythonw2.5
python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
To know which version is used by default, type
which python
To use another one, use the full path when invoking them:
/usr/bin/python/python2.5 myfile.py
/usr/bin/python/python2.6 myfile.py
etc.
If you want to change the default, you can create an alias in ~/.bashrc for example:
my_python='/usr/bin/python/python2.7'
or better change the link direction of /usr/bin/python (or whatever comes from which python). It is a link, so link it to the exact version you need.
ln -s /prefered/path/of/python /usr/bin/python
The files you see in /usr/bin/python are executables. You can do the following to see this:
file /usr/bin/python/python2.5
In my case:
$ file /usr/bin/python
/usr/bin/python: symbolic link to `python2.7'
$ file /usr/bin/python2.7
/usr/bin/python2.7: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xf66a10f2c444b2329b25ab6790abb7fbb4fe3f78, stripped
python --version
Then head on over to your .bashrc (should be in your home directory) and add:
alias python='[your path]'
for me I have:
alias python='/usr/bin/python2.7'
Just make sure you say:
source ./bashrc
to apply your changes.
Does anyone know where i can find this python module 'contextlib'?
root#overo:~# python
Python 2.6.6 (r266:84292, Mar 9 2011, 10:05:36)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named contextlib
I did not compile python myself personally. I'm just in this mess. It's running on an ARM based CPU so maybe some things were left out to save space.
I ran find / | grep contextlib which resulted in nothing.
Can i download this module from somewhere and just plonk it in /usr/lib/python2.6? Will that work?
I got this error in a different way.
I created a pipenv virtual environment using the 32bit version of Python 3.6.5 on Windows 10. I then realized I needed the 64bit version. Uninstalled the 32bit, installed the 64bit, and then tried to go back to my existing virtual env. The previously created env was now broken in odd ways and gave me this error.
I solved this by removing the old pipenv pipenv --rm and creating a new one with the newly installed version of python.
As others have noted, that module should be in the standard library, but if it's an embedded device, it may have been dropped to save space (if true, a foolish choice IMO, since leaving out contextlib.contextmanager robs the with statement of much of its power and convenience)
If you can name the specific device or manufacturer (or ask the vendor directly), you may be able to get a better answer.
As far as fixing it goes, grabbing http://hg.python.org/cpython/file/2.6/Lib/contextlib.py and dropping it in sys.path somewhere should do the trick (running python -m site will dump the list of directories that you can use)
It has been part of the standard library since 2.5 according to the docs. It seems a bit weird that you don't have it, it works with 2.6.6 for me (Ubuntu 10.10):
blair#blair-eeepc:~$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextlib
>>> contextlib.__file__
'/usr/lib/python2.6/contextlib.pyc'
Somebody may have a better suggestion, but if it comes to it there is a link at the top of the documentation to the source code (which is Python, so you should be able to use it directly without any compilation or anything).
Edit: Unless, as Santiago Lezica suggested, you compiled your copy of Python manually, in which case it should be a simple matter of copying the module into the correct library path.
Edit for updated question: To the best of my knowledge, just dropping the source into a directory on the Python path should work. You could do this in the system library, but, to avoid it being deleted/replaced/otherwise borked in future updates, I'd recommend putting it in a separate directory and adding that directory to the Python path. You could put it under /usr/local, or somewhere in your home directory.
With Angsrom Linux, contextlib is included in the python-misc package. You can grab it by running:
opkg install python-misc
This won't, however, get you all expected python modules, so you may also want to install python-modules:
opkg install python-modules
I found one more occasion, which produces the same error.
I had made a virtual environment with python 3.6. After a updated my python version to 3.7 I tried to activate the old virtual environment and got this error.
The solution was to delete the old environment and recreate it with the new python version.
Check sys.path to make sure your python interpreter is looking in the right directories. It should look something like this (not necessarily identical):
>>> import sys
>>> sys.path
['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/pymodules/python2.6']
EDIT: With the updated information in the question that this is an install of unknown origin on a constrained device, assuming that unnecessary modules were removed to save space makes sense. However, for the record, I'll mention another, perhaps more common scenario where modules cannot be found: when there are file permissions issues. For example:
$ python -c 'import contextlib; print(contextlib.__file__)'
/usr/lib/python2.6/contextlib.pyc
$ ls -l /usr/lib/python2.6/contextlib.py*
-rw-r--r-- 1 root root 4136 Dec 26 16:42 /usr/lib/python2.6/contextlib.py
-rw-r--r-- 1 root root 4127 Jan 1 21:45 /usr/lib/python2.6/contextlib.pyc
$ sudo chmod go-r /usr/lib/python2.6/contextlib.py*
$ python -c 'import contextlib; print(contextlib.__file__)'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named contextlib
Especially with custom installations, import problems due to file permission issues and path problems are some of the easiest things to check and, usually, to fix.
Python 2
sudo apt-get install python-contextlib2
Python 3
sudo apt-get install python3-contextlib2
contextlib was introduced in Python 2.5, can you remove and re-install your Python 2.6.6 again? From my copy of Python 2.6.6:
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import contextlib
>>>