I'm trying to run some python code with the sudo command, but every time I do it, it gives me an Import error. However, if I run, say, import numpy in terminal it gives me no errors. Also, if I build a code with several Imports and then run it without the sudo command, it gives me no errors and the code runs flawlessly. I already added Defaults env_keep += "PYTHONPATH" to the sudoers folder, so that's not the problem. I installed Anaconda3, so maybe that's useful information?
I'm running GNOME Ubuntu 16.04.1 LTS. And kernel version 4.4.0-59-generic.
I'm sorry, I'm very new at this, but I'm learning.
I ran which python and then I ran sudo which python and they gave me different directories.
sudo which python gave me usr/bin/python which python gave me home/user/anaconda3/bin/python
I tried running sudo ./anaconda3/envs/ml/bin/python doc.py but now it says that it can't find the file.
I'm running it with sudo because I need the permission for docker to work.
EDIT: trying sudo -E instead of sudo yields the same error.
The problem you have is that sudo does not follow the usual PATH order when looking at an executable: it searches the system directories first. This is written in the man sudo:
SECURITY NOTES
sudo tries to be safe when executing external commands.
To prevent command spoofing, sudo checks "." and "" (both denoting current directory) last when searching for a command in the
user's PATH (if one or both are in the PATH). Note, however, that the actual PATH environment variable is not modified and is passed
unchanged to the program that sudo executes.
So, to fix this you have to make sure that the command you give to sudo cannot match a system executable, i.e. specify the absolute path:
sudo /home/user/anaconda3/bin/python
A general command that should work is:
sudo "$(which python)"
This is because which python is executed before sudo, and its output is passed as an argument to sudo. However sudo by default does not perform any "shell-like" setup, and may restrict the environment, so you may consider using the -E or -i flags to make sudo pass the environment untouched and do the proper shell setup.
Related
I'm aware there are many similar questions but I have been through them all to no avail.
On Ubuntu 18.04, I have Python 2 and Python 3.6. I create a venv using the command below and attempt to install a package using pip. However, it attempts to install on the global system and not in the venv.
python3 -m venv v1
When I run 'which python' it correctly picks the python within the venv. I have checked he v1/bin folder and pip is installed. The path within the pip script is correctly pointed to toward python in the venv.
I have tried reinstalling python3 and venv, destroying and recreating the virtual environment and many other things. Wondering is there some rational way to understand and solve this.
The problem in my case was that the mounted drive I was working on was not mounted as executable. So pip couldn't be executed from within the venv on the mount.
This was confirmed because I was able to get a pip install using 'python -m pip install numpy' but when importing libraries, e.g. 'import numpy', was then faced with further error of:
multiarray_umath.cpython-36m-x86_64-linux-gnu.so: failed to map segment from shared object
which led back to the permissions issue as per github issue below. Fix for that by dvdabelle in comments then fixes dependent and original issue.
https://github.com/numpy/numpy/issues/15102
In his case, he could just switch drive. I have to use this drive. So the fix was to unmount my /data disk where I was working and remount it with exec option!
sudo umount /data
sudo mount -o exec /dev/sda4 /data
'which pip' now points to the pip in the venv correctly
Note: to make it permanent add the exec switch to the line for the drive in fstab as per https://download.tuxfamily.org/linuxvillage/Informatique/Fstab/fstab.html (make exec the last parameter in the options or user will override it) E.g.
UUID=1332d6c6-da31-4b0a-ac48-a87a39af7fec /data auto rw,user,auto,exec 0 0
I've just installed compiledb , OS: RHEL6
pip install --user compiledb
running it from the command line doesn't work:
$ compiledb
tells me that no compiledb command found.
I also tried
python comppiledb
the error was something like compiledb is not a script.
probably I should manually add user site-packages directory to some (?) path? what is a right way?
I know that's newbie question, but I couldn't find similar problem by googling.
and - yes, I don't know python just need (compildb) to convert makefile to compilation database, not more.
Let's look at the second case first. Try adding the -m flag when you run it with python:
python -m compiledb
To get it running as compiledb you probably need to add the pip user binary directory to your PATH. Let's see where pip install --user puts libraries on your machine. Run this on the command line:
python -c 'import site; print(site.USER_BASE)'
On my system this prints
/home/chris/.local
and binaries installed via pip install --user live in
/home/chris/.local/bin
Assuming you get similar output, you should be able to run compiledb as
/home/amigo421/.local/bin/compiledb
If that works you may want to add /home/amigo421/.local/bin to your PATH, e.g. by adding something like
export PATH="$PATH:/home/amigo421/.local/bin"
to your ~/.bash_profile then logging out and back in again. At that point you should be able to simply run
compiledb
I am installing Python 3.7.2 for the first time, and I'm using the VS Code python extension.
When I run python -V I get Python 2.7.10 which is not correct!
When I select the usr/local/bin/python3 interpreter in VS Code I get this error when running a script:
bash: /Users/erik/Work/Python/usr/local/bin/python3: No such file or directory
But when I look in usr/local/bin I can see that Python3 is there. I'm not sure why VS Code pastes the work directory in front of usr/local/bin ?
My first thought was that Python3 should be in the PATH variable so I ran the included Update Shell Profile command, which gives this feedback:
This script will update your shell profile when
the 'bin' directory of python is not early enough
of the PATH of your shell.
All right, you're a python lover already
Now, after rebooting VS Code I get a new option for selecting an interpreter:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
Is that different from the Python in usr/local/bin ? When I select it, I get this error:
The script isort is installed in '/Users/erik/Library/Python/3.7/bin' which is not on PATH.
I also get this sideways related error:
You are using pip version 18.1. You should consider upgrading via the 'pip install --upgrade pip' command.
But, when following these instructions I get yet another error:
bash: pip: command not found
All in all, this process and the official documentation seem less than user-friendly? Apparently I'm required to dig deep through my mac's system files in the terminal before even writing one line of code.
Am I missing an essential step here?
I suggest that you use virtual environment for your project
first
pip install virtualenv
open cmd in your project directory that you open in VS-Code (it's important that vs-code sees this virtualenv folder that we will create)
mkvirtualenv my_env
and it will activate it automatically. if not run
my_env/bin/Scripts/activate or my_env/Scripts/activate
Then go open vs-code then select my_env for python interpreter
Well, if you want to change your default Python version for the whole system, it might break some applications that are depending on Python 2.
You can alias the commands by adding this line to your ~/.bash_profile:
$ alias python='python3'
The python command will now refer to python3.
If you want to execute the original Python (which refers to python2), you can escape the alias (so \python will launch python2 without touching the alias).
Btw.
$ unlink /usr/local/bin/python
$ ln -s /usr/local/bin/python3.7 /usr/local/bin/python
could also be a workaround for you.
I've got a Python script that I need to run upon start up and problem
is that it throws an error saying "no module named xyz". I'm using external library which I installed using pip3. The script works just fine on its own
but I get aforementioned error when I want to run it right after boot.
What should I do ? I tried to delay importing the library with time.sleep(10) in case third-party libraries need few more seconds to load up after boot, but that didn't have desired effect.
I run the script on Raspberry Pi with Debian-based os called Raspbian. I configured execution upon start up by adding this sudo python3 script.py into /etc/profile file.
I don't see how pip install without sudo could have worked.
What I see: scripts run on startup from cron or /etc/profile are run under root, not under pi user. Thus, they don't have the same $PATH, $PYTHONPATH and other environment variable values that you have in your user's shell.
As pip install managed to run without sudo, I suspect that you installed your module into a user-specific directory, which is not a part of root's Python environment.
Replacing the /etc/profile line with sudo -u pi python3 script.py may help.
Whatever it is, it's the difference that is already in the Python environment. Waiting for 10s "for whatever to come up" will not help it.
1 - Download anaconda: https://www.anaconda.com/download/
2 - Create an environment in conda: https://conda.io/docs/user-guide/tasks/manage-environments.html
3 - Activate that environment
4 - conda install or pip3 install your package
It should just work.
In attempting to get started learning and developing python, I've tried to follow the Python Guide to installing python on OS X, but haven't found it particularly "noob friendly." I have a new MacBook (Mtn. Lion - OS X 10.8.3) wich comes with Python 2.7.2 built in. But the guide advises installing a "framework-style build" via homebrew. So:
I installed homebrew via ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
Then it tells you to add homebrew to the top of your PATH by adding it to your ~/.bashrc file. ls -a showed that I did not have a ~/.bashrc file in my home directory. After searching Stack Overflow on how to do that, I ran nano ~/.bashrc, and inserted the line export PATH=/usr/local/bin:$PATH to the file and saved the file.
I then ran brew install python --framework and the install completed.
Then, the guide says to "add the new Python scripts directory to your PATH" so, I'm assuming that means I need to add the line it provides to my ~/.bashrc file also. So, I added export PATH=/usr/local/share/python:$PATH to my ~/.bashrc file above my previous entry.
Finally, this is where I run into trouble, it says to easy_install pip. However, when I do that I get an error 13.
So, here are the things I need some help with.
Was I correct in my assumptions about how to add homebrew and python scripts to my PATH?
Did I do something wrong or do I just need to use sudo to install pip? (I'm really sorry if the answer is already on this page but even those answers don't make total sense to me and I want to be careful and not screw something up)
After installing the framework-style build of python (which I believe was the current 2.7.3), how come running python in my terminal still shows v2.7.2?
Thanks! I appreciate any help.
I've tried to follow the Python Guide to installing python on OS X,
but haven't found it particularly "noob friendly.
Yes, I think it is misleading/outdated.
Then it tells you to add homebrew to the top of your PATH by adding it
to your ~/.bashrc file. ls -a showed that I did not have a ~/.bashrc
file in my home directory. After searching Stack Overflow on how to do
that, I ran nano ~/.bashrc, and inserted the line export
PATH=/usr/local/bin:$PATH to the file and saved the file.
On the Mac, just use ~/.profile
I then ran brew install python --framework and the install completed.
I think you don't need the --framework option unless you want to replace your Mac OS default installation and need an Mac OS Framework-style directory layout. There is no need to replace it though, the homebrew installation will take precedence anyway.
Then, the guide says to "add the new Python scripts directory to your
PATH" so, I'm assuming that means I need to add the line it provides
to my ~/.bashrc file also. So, I added export
PATH=/usr/local/share/python:$PATH to my ~/.bashrc file above my
previous entry.
Again, do it in ~/.profile. And don't forget to do a
source ~/.profile
otherwise the changes will only become active in any new terminal window, not the one you are currently using.
Finally, this is where I run into trouble, it says to easy_install
pip. However, when I do that I get an error 13.
The error shows that you try to install it your Mac OS system's default Python library (rather than in /usr/local, homebrew style), which would require root privileges. Just don't.
Also, with homebrew python, pip is already installed.
Check your path:
$ which pip
/usr/local/bin/pip
$ ls -l /usr/local/bin/pip
[..] /usr/local/bin/pip -> ../Cellar/python/2.7.3/bin/pip
Added bonus: Then do
pip install virtualenv
and use that.
And to your questions:
Was I correct in my assumptions about how to add homebrew and python
scripts to my PATH?
Yes, but use .profile and do a source .profile afterwards.
Did I do something wrong or do I just need to use sudo to install pip?
(I'm really sorry if the answer is already on this page but even those
answers don't make total sense to me and I want to be careful and not
screw something up)
You don't need sudo with homebrew, and pip is installed automatically with homebrew python.
After installing the framework-style build of python (which I believe
was the current 2.7.3), how come running python in my terminal still
shows v2.7.2?
Probably PATH not correct, do echo $PATH and check that it is correct. That is unrelated to being "framework-style" or not, though.
If you installed python with homebrew, you should already have pip installed. Try running
pip --version
to see whether and where pip is installed. Hopefully it's in a /usr/local/... path where your other homebrew things live.
Also before you install too much more with homebrew be sure to run these commands:
brew update
brew doctor
Good luck!