I want to access files and directories which may require root permission from a command line call to a python script using Enthought Canopy Python. However running python as root gives me the system distributed python.
$ python
Enthought Canopy Python 2.7.6 | 64-bit | (default, Sep 15 2014, 17:36:10)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
$ sudo 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.
Same results using default bash terminal (Ubuntu Linux 14.04) and using the canopy command line (Canopy 1.5.2).
I reinstalled Canopy as root (i.e. re-ran the shell script as root) and the same problem still occurs.
Is there an easy way to run Canopy as root? I am not trying to distribute it to a number of users on a system just run a python script on files that need root permissions.
It sounds like sudo is not using your current PATH settings to locate the executable to run. IIRC this is a configuration option for sudo, whether to use a limited default PATH or not.
An easy way to work around this is to specify the full path to the python excutable that you want to execute. For example, if you've activated a Canopy virtual environment, then you can do it like this:
$ sudo $VIRTUAL_ENV/bin/python
Related
I'm trying to run the benchmarks game repo bencher script which was written in python 2 and requires the gtop module, NOT the pygtop module. After searching everywhere and even following their README.md I could not figure out how to get this in my python 2.7.18 virtual environment (created and maintained using pyenv).
I decided to have a look at my system version of python 2.7.18 as I followed the guide from this SO reply and the packages downloaded/installed successfully. My system version of python can import the module just fine:
Python 2.7.18 (default, Mar 8 2021, 13:02:45)
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtop
>>> gtop.__file__
'/usr/lib/python2.7/dist-packages/gtk-2.0/gtop.so'
So apparently it comes as a .so which I've researched is like a DLL library for Ubuntu (correct me if I'm wrong)?
Is there a way for me to just copy this into my virtual environment of the same python version?
Thank you to #SamBob for suggesting the SO reply that led to the answer.
What I've been mistakenly doing is copying the gtk-2.0 directory into my virtualenvs site-packages such that:
$HOME/.pyenv/versions/<venv>/lib/python2.7/site-packages/gtk-2.0/gtop.so
What I've done is instead copy just the gtop.so library into the virtualenvs site-packages, and it seemed to recognise it no problem, such that
$HOME/.pyenv/versions/<venv>/lib/python2.7/site-packages/gtop.so
Here is my output now:
Python 2.7.18 (default, Oct 18 2021, 23:18:58)
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtop
>>> gtop.__file__
'/home/muffin/.pyenv/versions/project-2.7.18/lib/python2.7/site-packages/gtop.so'
>>>
I am now configuring remote interpreter when I use Pycharm 2017.3. My remote Python2.7 interpreter's path is /opt/apps/python/2.7.10/bin/python2.7, but when I use ssh to configure remote interpreter, Pycharm tells me this interpreter is Python 2.7.5!!! And I can't use it to run my code! (because i don't download relevant libs under 2.7.5) More interesting, when I enter /opt/apps/python/2.7.10/bin/python2.7 on my terminal (mac shell), it shows:
Python 2.7.10 (default, Apr 7 2017, 10:43:09)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Woo, i dont know the reason~ Who can tell me? Thank you!
~$ python
bash: python: command not found
while running python2 and python 3 its showing different versions.
~$ python2
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
~$ python3
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
i think somehow default python link is broken. i can't seem to access ubuntu software center also.
Please suggest a way to fix this.
Rather than re-creating the simlink as suggested, I would start working with virtualenv. You can create virtual environment pointing at different python versions by doing:
virtualenv -p /usr/bin/python2 venv_name (for Python 2.7)
virtualenv -p /usr/bin/python3 venv_name (for Python 3)
Once you go into the venv and do the source venv_name/bin/activate, your python symbolic link will be pointed at the version you've chosen when creating it.
Also, you might find a pyvenv link which will be associated to Python 3 as this version of Python comes with virtualenv (and pip) out of the box. You might need to install virtualenv with pip (or easy install), and to figure it out which Pythonversion is your pip associated to, you can do pip --version
OSX. When I installed the Canopy IDE, Enthought Canopy Python 2.7.6 was set as the default Python for the bash shell:
$ python
Enthought Canopy Python 2.7.6 | 32-bit | (default, Sep 15 2014, 17:36:57)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Currently, I am using another IDE (PyCharm) for a project where a Python module is needed. I tried to download the module directly from the bash shell using pip but it did not work:
pip install psycopg2 <---module
I believe it has something to do with Enthought Canopy Python running, so I attempted to restore the original python. However, I could not restore it. The instructions from this Enthought post (2011) did not work:
OS X - After EPD installation, restore system python to be default
They say to comment out the following lines from the ~/.bash_profile file, save the changes and restart the bash shell:
# PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}"
# export PATH
Didn't work.
Hi there I have downloaded the mac installer here, http://www.python.org/download/releases/3.1.2/ , & installed it. But when I run terminal & type python it says:
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
What I want to know is, is it safe to run Update Shell Profile.command in the Python 3.1 folder ? or should I run python 3.1.2 separately ? If I should run python 3.1.2 separately, how do I do so ? also how do I start IDLE ?
Is there another python executable, perhaps python31?
You can also install other python versions via MacPorts if you need (although you'll still have to choose the right executable).
This should also be relevant: Multiple versions of Python on OS X Leopard