When using Python on an interactive shell I'm able to import the cx_Oracle file with no problem. Ex:
me#server~/ $ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>
As you can see, importing works without a hitch. However, when I try to run a Python script doing the same thing, I get an error:
me#server~/ $ sudo script.py
Traceback (most recent call last):
File "/usr/local/bin/script.py", line 19, in <module>
import cx_Oracle
ImportError: No module named "cx_Oracle'
Here is the important section from script.py:
# 16 other lines above here
# Imports
import sys
import cx_Oracle
import psycopg2
...
I'm befuddled here. Other pertinent information is the server I'm running is Ubuntu 14.04.1 LTS (upgraded from 12.04) 64bit. which python and sudo which python both point to the same location. Also, doing this as root via sudo su - gets the same results; import OK from interactive but error from script.
Nothing other than the OS upgrade happened between when this worked and when it stopped working.
Sorry, all. This was a silly on my part. Turns out the script in question was using Python3, and when the server upgraded, Python3 went from being 3.2 version to being 3.4 version.
Once the cx_Oracle module was set up in the 3.4 version, everything worked as expected.
Phil, your final note talking about the shebang was what lead me to discover this, so kudos to you! The reason I didn't mark your response as the answer was because technically it wasn't but led me on the right path.
Cheers!
sudo starts a new bash environment which is then pointing to a different python executable (different installed modules).
You can verify this with which python and sudo which python
EDIT: so if they point to the same executable, then you should look at sys.path to find differences. In both environemnts you can:
python -c "import sys; print('\n'.join(sys.path))"
sudo python -c "import sys; print('\n'.join(sys.path))"
Look for differences. If there are none:
A common error in import situations like this is that python will first look at the local dir. So if you happen to be running python and importing something what is found locally (i.e. cx_Oracle is a subdir of your current location), you will get an import error if you change directories.
Final note: I have assumed here that the shbang of the script.py points to the same executable as which python. That is, that python script.py and script.py return the same error.
I would like to get the parent user after a sudo command in Python
For example, from the shell I can do:
# Shows root; *undesired* output
$ sudo whoami
root
# Shows parent user sjcipher, desired output
$ sudo who am i
sjcipher
How do I do this in Python without using an external program?
SUDO_USER environmental variable should be available in most cases:
import os
if os.environ.has_key('SUDO_USER'):
print os.environ['SUDO_USER']
else:
print os.environ['USER']
who am i gets it's information from utmp(5);
with Python you can access with information with pyutmp;
Here's an example, adapted from the pyutmp homepage:
#!/usr/bin/env python2
from pyutmp import UtmpFile
import time, os
mytty = os.ttyname(os.open("/dev/stdin", os.O_RDONLY))
for utmp in UtmpFile():
if utmp.ut_user_process and utmp.ut_line == mytty:
print '%s logged in at %s on tty %s' % (utmp.ut_user, time.ctime(utmp.ut_time), utmp.ut_line)
$ ./test.py
martin logged in at Tue Jul 1 21:38:35 2014 on tty /dev/pts/5
$ sudo ./test.py
martin logged in at Tue Jul 1 21:38:35 2014 on tty /dev/pts/5
Drawbacks: this is a C module (ie. it requires compiling), and only works with Python 2 (not 3).
Perhaps a better alternative is using of the environment variables that sudo offers? For example:
[~]% sudo env | grep 1001
SUDO_UID=1001
SUDO_GID=1001
[~]% sudo env | grep martin
SUDO_USER=martin
So using something like os.environ['SUDO_USER'] may be better, depending on what you're exactly trying to do.
Depending on the setup you could use the environment variables that have been set. Note, that this may not work in all cases but may in yours. Should return the original user before su.
import os
print os.environ["USER"]
Here's a one-liner that will do it.
user = os.environ['SUDO_USER'] if 'SUDO_USER' in os.environ else os.environ['USER']
Either os.getlogin() or os.getenv('SUDO_USER') look like good choices.
[vagrant#localhost ~]$ sudo python3.6
Python 3.6.3 (default, Oct 11 2017, 18:17:37)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getlogin()
'vagrant'
>>> os.getenv('SUDO_USER')
'vagrant'
>>> os.getenv('USER')
'root'
>>> import getpass
>>> getpass.getuser()
'root'
as referenced before, you can use getpass module
>>> import getpass
>>> getpass.getuser()
'kostya'
I'm trying to install Anaconda on my Windows 7 machine. I often use cygwin to for my command-line work, and I would like to manage Anaconda from there. I've worked through the graphic installer without any issues, and checked necessary boxes to reset my default path to this install of python. I go ahead to check where python is and initially I get this...
$ which python
/usr/bin/python
From here python works fine...
$ python
Python 2.7.5 (default, Oct 2 2013, 22:34:09)
[GCC 4.8.1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
But I'm trying to work from anaconda, so I should just need to redefine my path...
$ export PATH=/cygdrive/c/anaconda:$PATH
$ which python
/cygdrive/c/anaconda/python
And now I should be good to go, but when I try and step into python, it just hangs
$ python
Any idea why this might be happening? verbose return, below...
$ python -v
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# C:\anaconda\lib\site.pyc matches C:\anaconda\lib\site.py
import site # precompiled from C:\anaconda\lib\site.pyc
# C:\anaconda\lib\os.pyc matches C:\anaconda\lib\os.py
import os # precompiled from C:\anaconda\lib\os.pyc
import errno # builtin
import nt # builtin
# C:\anaconda\lib\ntpath.pyc matches C:\anaconda\lib\ntpath.py
import ntpath # precompiled from C:\anaconda\lib\ntpath.pyc
# C:\anaconda\lib\stat.pyc matches C:\anaconda\lib\stat.py
import stat # precompiled from C:\anaconda\lib\stat.pyc
# C:\anaconda\lib\genericpath.pyc matches C:\anaconda\lib\genericpath.py
import genericpath # precompiled from C:\anaconda\lib\genericpath.pyc
# C:\anaconda\lib\warnings.pyc matches C:\anaconda\lib\warnings.py
import warnings # precompiled from C:\anaconda\lib\warnings.pyc
# C:\anaconda\lib\linecache.pyc matches C:\anaconda\lib\linecache.py
import linecache # precompiled from C:\anaconda\lib\linecache.pyc
# C:\anaconda\lib\types.pyc matches C:\anaconda\lib\types.py
import types # precompiled from C:\anaconda\lib\types.pyc
# C:\anaconda\lib\UserDict.pyc matches C:\anaconda\lib\UserDict.py
import UserDict # precompiled from C:\anaconda\lib\UserDict.pyc
# C:\anaconda\lib\_abcoll.pyc matches C:\anaconda\lib\_abcoll.py
import _abcoll # precompiled from C:\anaconda\lib\_abcoll.pyc
# C:\anaconda\lib\abc.pyc matches C:\anaconda\lib\abc.py
import abc # precompiled from C:\anaconda\lib\abc.pyc
# C:\anaconda\lib\_weakrefset.pyc matches C:\anaconda\lib\_weakrefset.py
import _weakrefset # precompiled from C:\anaconda\lib\_weakrefset.pyc
import _weakref # builtin
# C:\anaconda\lib\copy_reg.pyc matches C:\anaconda\lib\copy_reg.py
import copy_reg # precompiled from C:\anaconda\lib\copy_reg.pyc
# C:\anaconda\lib\traceback.pyc matches C:\anaconda\lib\traceback.py
import traceback # precompiled from C:\anaconda\lib\traceback.pyc
# C:\anaconda\lib\sysconfig.pyc matches C:\anaconda\lib\sysconfig.py
import sysconfig # precompiled from C:\anaconda\lib\sysconfig.pyc
# C:\anaconda\lib\re.pyc matches C:\anaconda\lib\re.py
import re # precompiled from C:\anaconda\lib\re.pyc
# C:\anaconda\lib\sre_compile.pyc matches C:\anaconda\lib\sre_compile.py
import sre_compile # precompiled from C:\anaconda\lib\sre_compile.pyc
import _sre # builtin
# C:\anaconda\lib\sre_parse.pyc matches C:\anaconda\lib\sre_parse.py
import sre_parse # precompiled from C:\anaconda\lib\sre_parse.pyc
# C:\anaconda\lib\sre_constants.pyc matches C:\anaconda\lib\sre_constants.py
import sre_constants # precompiled from C:\anaconda\lib\sre_constants.pyc
# C:\anaconda\lib\locale.pyc matches C:\anaconda\lib\locale.py
import locale # precompiled from C:\anaconda\lib\locale.pyc
import encodings # directory C:\anaconda\lib\encodings
# C:\anaconda\lib\encodings\__init__.pyc matches C:\anaconda\lib\encodings\__init__.py
import encodings # precompiled from C:\anaconda\lib\encodings\__init__.pyc
# C:\anaconda\lib\codecs.pyc matches C:\anaconda\lib\codecs.py
import codecs # precompiled from C:\anaconda\lib\codecs.pyc
import _codecs # builtin
# C:\anaconda\lib\encodings\aliases.pyc matches C:\anaconda\lib\encodings\aliases.py
import encodings.aliases # precompiled from C:\anaconda\lib\encodings\aliases.pyc
import operator # builtin
# C:\anaconda\lib\functools.pyc matches C:\anaconda\lib\functools.py
import functools # precompiled from C:\anaconda\lib\functools.pyc
import _functools # builtin
import _locale # builtin
# C:\anaconda\lib\encodings\cp1252.pyc matches C:\anaconda\lib\encodings\cp1252.py
import encodings.cp1252 # precompiled from C:\anaconda\lib\encodings\cp1252.pyc
# zipimport: found 13 names in C:\anaconda\lib\site-packages\runipy-0.1.0-py2.7.egg
# zipimport: found 144 names in C:\anaconda\lib\site-packages\setuptools-3.6-py2.7.egg
Python 2.7.7 |Anaconda 2.0.1 (64-bit)| (default, Jun 11 2014, 10:40:02) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and htt
Another (possibly related) issue I'm having is needing to reset the path every time I close/open cygwin. I've entered the following text into .bashrc and .profile to try and set the path permanently:
# Set path to python from anaconda install
export PATH=/cygdrive/c/anaconda:$PATH
After opening and closing cygwin, I return to:
$ which python
/usr/bin/python
Could this be related to setting certain system environment variables?
To work with the interactive Python shell in Cygwin I use the -i option.
To get it from the Anaconda install, I used the steps suggested above:
$ export PATH=/cygdrive/c/anaconda:$PATH
$ which python
/cygdrive/c/anaconda/python
Then I launch python within Cygwin with the -i option:
$ python -i
Python 2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Jul 2 2014, 15:12:11) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>>>
The libraries are also working fine. For instance the pandas library (which has been installed through Anaconda) can be imported now.
>>>> import pandas
>>>> pandas.DataFrame
<class 'pandas.core.frame.DataFrame'>
Now to make this permanent I added the path in my bashrc file:
$ echo 'export PATH=/cygdrive/c/anaconda:$PATH' >> .bashrc
$ source .bashrc
$ which python
/cygdrive/c/anaconda/python
I hope this helps.
I too was having an issue getting anaconda set up with my Cygwin / Windows 7 system. What worked was the following:
Edited the ~/.bashrc. Add below to the bottom of file.
export PATH=/cygdrive/c/Anaconda:$PATH
This mapped Cygwin's python to the anaconda distribution as proof by entering which python in Cygwin's console. However, when launching python it would hang up forcing me to ^C out of the command. I found that python -i will launch the interactive python interpreter with no issues.
Path
Regarding your path issue, see Installing anaconda to use with windows on how to fix that. Alternatively, you can just run /cygdrive/c/Anaconda/python.exe to start the Anaconda/windows python and leave the standard python pointing to the cygwin version.
Note that few of the suggestions given on the internet also include the necessary change to PYTHONPATH: if you use the anaconda/windows python, you also want the anaconda python libraries. Binary modules like numpy will surely crash or misbehave if you mix things up. The easiest way to check this is to install a module in cygwin that you do not need in Conda and test whether you can import that from anaconda python. If you can, your path is wrong.
Anaconda python will automatically add c:\Anconda to your PYTHONPATH, but if you have it set to serve CYGWIN python libraries, those will be found before the Anaconda libs. Try
import sys
sys.path
Terminal
However, this is also a terminal issue: Anaconda (Windows) python expects a windows shell and a modern cygwin shell is like an xterm, so very different. Diagnose this by running python --help, if that works but plain python hangs, the interactive prompt is the problem. This means you can run python programs, you just cannot interact with them.
A way around this is to use the cygwin bash shell instead of the cygwin terminal (both are under Cygwin group in the Start menu). They look very similar, but the font in the bash shell is less pretty and you cannot make the window wider than 80 chars (like all other CMD windows, never understood that 'feature'). This is cygwin bash running inside a CMD window, and your anaconda python will be interactive. The cygwin terminal gives you a bash shell running in a putty derivative, which mimics an xterm and cannot be used by anaconda python.
There is another problem with conda's interpretation of cygwin. When you source the activation script for an environment, it prepends e.g.
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/mingw-w64/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/usr/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Scripts
to your PATH. However, these are not proper directories in cygwin.
I fixed this with
ln -s /cygdrive/c /C
and then if you create conda environments with specific versions of python they should work.
Regarding setting your path permanently, edit the .profile in your home directory (it's a hidden file).
.profile (other names are also valid, see the bash man page) contains bash commands. It is executed when bash is started as login shell.
At the bottom of this file, put your set path command:
export PATH=/cygdrive/c/anaconda:$PATH
As for why python is hanging, please provide more information - for example what's the output of python -v?
I'm a little late in coming to this, but I was having the same problem as you #thomasshouler. Try adding the export line below to the end of the .bash_profile and .profile files as well as your .bashrc file (as mentioned above). Make sure to "source .bash_profile" etc for each file.
export PATH=/cygdrive/c/anaconda:$PATH
Once the Cygwin path is updated as mentioned above, you can also add an alias in the .bashrc file:
alias python='python -i'
This will bring up the Anaconda python interpreter in interactive mode from a Cygwin bash prompt
Found that for Anaconda 4, do:
vim /home/[Username]/.bash_profile
Append to the last line:
PATH=$PATH:/cygdrive/c/Users/[Username]/Anaconda3/Scripts/
This solution worked for me as the usual anaconda3/bin could not be found.
How do I check which version of the Python interpreter is running my script?
This information is available in the sys.version string in the sys module:
>>> import sys
Human readable:
>>> print(sys.version) # parentheses necessary in python 3.
2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)]
For further processing, use sys.version_info or sys.hexversion:
>>> sys.version_info
(2, 5, 2, 'final', 0)
# or
>>> sys.hexversion
34014192
To ensure a script runs with a minimal version requirement of the Python interpreter add this to your code:
assert sys.version_info >= (2, 5)
This compares major and minor version information. Add micro (=0, 1, etc) and even releaselevel (='alpha','final', etc) to the tuple as you like. Note however, that it is almost always better to "duck" check if a certain feature is there, and if not, workaround (or bail out). Sometimes features go away in newer releases, being replaced by others.
From the command line (note the capital 'V'):
python -V
This is documented in 'man python'.
From IPython console
!python -V
Use platform's python_version from the stdlib:
from platform import python_version
print(python_version())
# 3.9.2
I like sys.hexversion for stuff like this.
>>> import sys
>>> sys.hexversion
33883376
>>> '%x' % sys.hexversion
'20504f0'
>>> sys.hexversion < 0x02060000
True
Your best bet is probably something like so:
>>> import sys
>>> sys.version_info
(2, 6, 4, 'final', 0)
>>> if not sys.version_info[:2] == (2, 6):
... print "Error, I need python 2.6"
... else:
... from my_module import twoPointSixCode
>>>
Additionally, you can always wrap your imports in a simple try, which should catch syntax errors. And, to #Heikki's point, this code will be compatible with much older versions of python:
>>> try:
... from my_module import twoPointSixCode
... except Exception:
... print "can't import, probably because your python is too old!"
>>>
Put something like:
#!/usr/bin/env/python
import sys
if sys.version_info<(2,6,0):
sys.stderr.write("You need python 2.6 or later to run this script\n")
exit(1)
at the top of your script.
Note that depending on what else is in your script, older versions of python than the target may not be able to even load the script, so won't get far enough to report this error. As a workaround, you can run the above in a script that imports the script with the more modern code.
Here's a short commandline version which exits straight away (handy for scripts and automated execution):
python -c "print(__import__('sys').version)"
Or just the major, minor and micro:
python -c "print(__import__('sys').version_info[:1])" # (2,)
python -c "print(__import__('sys').version_info[:2])" # (2, 7)
python -c "print(__import__('sys').version_info[:3])" # (2, 7, 6)
With six module, you can do it by:
import six
if six.PY2:
# this is python2.x
else:
# six.PY3
# this is python3.x
import sys
sys.version.split(' ')[0]
sys.version gives you what you want, just pick the first number :)
Like Seth said, the main script could check sys.version_info (but note that that didn't appear until 2.0, so if you want to support older versions you would need to check another version property of the sys module).
But you still need to take care of not using any Python language features in the file that are not available in older Python versions. For example, this is allowed in Python 2.5 and later:
try:
pass
except:
pass
finally:
pass
but won't work in older Python versions, because you could only have except OR finally match the try. So for compatibility with older Python versions you need to write:
try:
try:
pass
except:
pass
finally:
pass
Several answers already suggest how to query the current python version. To check programmatically the version requirements, I'd make use of one of the following two methods:
# Method 1: (see krawyoti's answer)
import sys
assert(sys.version_info >= (2,6))
# Method 2:
import platform
from distutils.version import StrictVersion
assert(StrictVersion(platform.python_version()) >= "2.6")
Just for fun, the following is a way of doing it on CPython 1.0-3.7b2, Pypy, Jython and Micropython. This is more of a curiosity than a way of doing it in modern code. I wrote it as part of http://stromberg.dnsalias.org/~strombrg/pythons/ , which is a script for testing a snippet of code on many versions of python at once, so you can easily get a feel for what python features are compatible with what versions of python:
via_platform = 0
check_sys = 0
via_sys_version_info = 0
via_sys_version = 0
test_sys = 0
try:
import platform
except (ImportError, NameError):
# We have no platform module - try to get the info via the sys module
check_sys = 1
if not check_sys:
if hasattr(platform, "python_version"):
via_platform = 1
else:
check_sys = 1
if check_sys:
try:
import sys
test_sys = 1
except (ImportError, NameError):
# just let via_sys_version_info and via_sys_version remain False - we have no sys module
pass
if test_sys:
if hasattr(sys, "version_info"):
via_sys_version_info = 1
elif hasattr(sys, "version"):
via_sys_version = 1
else:
# just let via_sys remain False
pass
if via_platform:
# This gives pretty good info, but is not available in older interpreters. Also, micropython has a
# platform module that does not really contain anything.
print(platform.python_version())
elif via_sys_version_info:
# This is compatible with some older interpreters, but does not give quite as much info.
print("%s.%s.%s" % sys.version_info[:3])
elif via_sys_version:
import string
# This is compatible with some older interpreters, but does not give quite as much info.
verbose_version = sys.version
version_list = string.split(verbose_version)
print(version_list[0])
else:
print("unknown")
If you want to detect pre-Python 3 and don't want to import anything...
...you can (ab)use list comprehension scoping changes and do it in a single expression:
is_python_3_or_above = (lambda x: [x for x in [False]] and None or x)(True)
from sys import version_info, api_version, version, hexversion
print(f"sys.version: {version}")
print(f"sys.api_version: {api_version}")
print(f"sys.version_info: {version_info}")
print(f"sys.hexversion: {hexversion}")
output
sys.version: 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
sys.api_version: 1013
sys.version_info: sys.version_info(major=3, minor=6, micro=5, releaselevel='final', serial=0)
sys.hexversion: 50726384
The simplest way
Just type python in your terminal and you can see the version
as like following
desktop:~$ python
Python 2.7.6 (default, Jun 22 2015, 18:00:18)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
sys.version_info doesn't seem to return a tuple as of 3.7. Rather, it returns a special class, so all of the examples using tuples don't work, for me at least. Here's the output from a python console:
>>> import sys
>>> type(sys.version_info)
<class 'sys.version_info'>
I've found that using a combination of sys.version_info.major and sys.version_info.minor seems to suffice. For example,...
import sys
if sys.version_info.major > 3:
print('Upgrade to Python 3')
exit(1)
checks if you're running Python 3. You can even check for more specific versions with...
import sys
ver = sys.version_info
if ver.major > 2:
if ver.major == 3 and ver.minor <= 4:
print('Upgrade to Python 3.5')
exit(1)
can check to see if you're running at least Python 3.5.
Check Python version: python -V or python --version or apt-cache policy python
you can also run whereis python to see how many versions are installed.
To verify the Python version for commands on Windows, run the following commands in a command prompt and verify the output
c:\>python -V
Python 2.7.16
c:\>py -2 -V
Python 2.7.16
c:\>py -3 -V
Python 3.7.3
Also, To see the folder configuration for each Python version, run the following commands:
For Python 2,'py -2 -m site'
For Python 3,'py -3 -m site'
This just returns 2.7, 3.6 or 3.9
import sys
current_version = ".".join(map(str, sys.version_info[0:2]))
which is what you usually you need...
The even simpler simplest way:
In Spyder, start a new "IPython Console", then run any of your existing scripts.
Now the version can be seen in the first output printed in the console window:
"Python 3.7.3 (default, Apr 24 2019, 15:29:51)..."
To check from the command-line, in one single command, but include major, minor, micro version, releaselevel and serial, then invoke the same Python interpreter (i.e. same path) as you're using for your script:
> path/to/your/python -c "import sys; print('{}.{}.{}-{}-{}'.format(*sys.version_info))"
3.7.6-final-0
Note: .format() instead of f-strings or '.'.join() allows you to use arbitrary formatting and separator chars, e.g. to make this a greppable one-word string. I put this inside a bash utility script that reports all important versions: python, numpy, pandas, sklearn, MacOS, xcode, clang, brew, conda, anaconda, gcc/g++ etc. Useful for logging, replicability, troubleshootingm bug-reporting etc.
all answers has great insights
another way is platform.python_version_tuple()
python3 -c “import platform; print(platform.python_version_tuple())”
(‘3’, ‘6’, ‘8’)
if you want to check the python version for at least condition (e.g., python 3.9.0):
import platform
python_version_tuple = list(map(int, platform.python_version_tuple()))
if python_version_tuple >= [3, 9, 0]:
# Running at least Python 3.9.0
else:
# Running Python below 3.9.0
If you are working on linux just give command python output will be like this
Python 2.4.3 (#1, Jun 11 2009, 14:09:37)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
For windows, Go to command prompt and type this command to get the python version:
python --version
Or
python -V
A attempt using os.popen to read it in a variable:
import os
ver = os.popen('python -V').read().strip()
print(ver)