I have a cli with a few functions that i copy into /local/bin using:
sudo ln -s ~/code/project/scripts/sl /usr/local/bin/sl
If I activate the virtual env and do sl command_name service it works as expected.
But if try to run it from terminal I get:
user#MacBook-Pro-USER ~ % sl command_name service
Traceback (most recent call last):
File "/usr/local/bin/sl", line 4, in <module>
from sl_commands.sl import execute
File "/Users/user/code/project/scripts/sl_commands/sl.py", line 1, in <module>
import argh
ImportError: No module named argh
Running pip3 install argh results in:
user#MacBook-Pro-USER ~ % pip3 install argh
Requirement already satisfied: argh in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (0.26.2)
If I try the same on ubuntu the script executes correctly. How do i make it run on MacOS without activating a virtualenv?
The shebang needs to be #! /usr/bin/env python3 and not python. Since python2 is 'python' on MacOS
Related
Trying to install third party modules on my mac using pip and I am getting this error (Using IDLE as the Python shell):
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
(Have tried multiple modules and all return the same error)
I have installed the modules using:
pip3 install pyperclip
I can see that it has installed into the directory:
/opt/homebrew/lib/python3.9/site-packages
Ive tried going through several of the posts posted on StackOverflow such as this and this but after trying their steps I cant seem to resolve it.
which -a pip3 gives:
/opt/homebrew/bin/pip3
/Library/Frameworks/Python.framework/Versions/3.9/bin/pip3
/usr/local/bin/pip3
/usr/bin/pip3
which -a python3 gives:
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
/usr/local/bin/python3
/usr/bin/python3
print(sys.version) gives:
3.9.1 (v3.9.1:1e5d33e9b9, Dec 7 2020, 12:44:01)
[Clang 12.0.0 (clang-1200.0.32.27)]
pip --version gives:
pip 22.1.2 from /opt/homebrew/lib/python3.9/site-packages/pip (python 3.9)
Running /usr/local/bin/python3 test1.py:
Traceback (most recent call last):
File "/Users/dxz/Downloads/test1.py", line 1, in <module>
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
Thanks
You can run this command for seeing pip3 uses which python:
pip3 --version
But i guess you are not choosing right python to run your .py file
if you run your .py like this:
python3 main.py
You must be sure pip3 installing libraries to 'python3' linked python or you can run your main.py file with one of pip3 linked python
I mean if pip3 --version gives you
/usr/bin/python3
this python path you can run your main.py like this
/usr/bin/python3 main.py
but if you want to use best practice i recommend to you using virtualenv
I am trying to install geopy in a virtual environment created by miniconda. When I do pip install geopy, it gives me this:
(finalenv) MacBook-Pro ~ % pip install geopy
Requirement already satisfied: geopy in /usr/local/Caskroom/miniconda/base/envs/finalenv/lib/python3.6/site-packages (2.2.0)
Requirement already satisfied: geographiclib<2,>=1.49 in /usr/local/Caskroom/miniconda/base/envs/finalenv/lib/python3.6/site-packages (from geopy) (1.52)
The python version for this Finalenv is Python 3.6.13 :: Anaconda, Inc.
However, when I try to run my script:
import psycopg2
import geopy
I get an error that:
(finalenv) MacBook-Pro ~ % /usr/local/bin/python3 /Users/s/Desktop/s-project/v2.py
Traceback (most recent call last):
File "/Users/s/Desktop/s/file.py", line 15, in <module>
import geopy
ModuleNotFoundError: No module named 'geopy'
what am I missing here?
Since you are using Anaconda, you can try installing it through conda to see if the installation happens on the same folder. Open Anaconda CLI prompt as Administrator. You can try
conda config --add channels conda-forge
and
conda install -c conda-forge geopy.
/usr/local/bin/python3 is your OS python executable, not the one used by your virtualenv.
In order to run the script always with the python3 on the virtual environment (if it is activated), you can do this:
Add #!/usr/bin/env python3 at the beginning of the file. It will look like this:
#!/usr/bin/env python3
import psycopg2
import geopy
Give execution permissions to the file - in your case: chmod u+x v2.py
And run the file from the shell: ./v2.py
in ubuntu 18.04, when i change default python from python 3.6 to other version by this command:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1
or when i remove python 3.6 and install other version netplan apply not working and result this error:
File "/usr/sbin/netplan", line 20, in <module>
from netplan import Netplan
File "/usr/share/netplan/netplan/__init__.py", line 18, in <module>
from netplan.cli.core import Netplan
File "/usr/share/netplan/netplan/cli/core.py", line 24, in <module>
import netplan.cli.utils as utils
File "/usr/share/netplan/netplan/cli/utils.py", line 25, in <module>
import netifaces
ModuleNotFoundError: No module named 'netifaces'
and command pip install netifaces has some errors.
I faced the same issue before with vagrant. If you use update-alternatives to make python3 alias points to another version of Python, vagrant will not work. You cannot use update-alternatives to change the alias of Python3.
For some reason I lost my python configuration after an update made by my ubuntu server,solved by:
Checking if I could import the module so on CLI:
python
import python
After getting the same message I realize my python environment was falling to use the modules even when it all showed up as installed I went ahead and "upgrade" the python modules:
python pip install --upgrade pip
python pip install --upgrade netifaces
python pip install --upgrade "any module you need to use for your script"
And just like that modules were recognized updated and properly install.
tl;dr: Create a link netifaces.so to netifaces.cpython-36m-x86_64-linux-gnu.so in /usr/lib/python3/
In /usr/lib/python3/dist-packages/ is only a module for python3.6:
vagrant#ubuntu18.04:~$ find /usr/lib/python3/dist-packages/ -type f -name netifaces\*
/usr/lib/python3/dist-packages/netifaces.cpython-36m-x86_64-linux-gnu.so
That's why importing 'netifaces' failes for python3.8 while it works for python3.6:
vagrant#ubuntu18.04:~$ python3.6 -c 'import netifaces; print("works!")'
works!
vagrant#ubuntu18.04:~$ python3.8 -c 'import netifaces; print("works!")'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'netifaces'
One can link to it with a more unspecific name so python3.8 can find and use it:
vagrant#ubuntu18.04:~$ sudo ln -s /usr/lib/python3/dist-packages/netifaces.cpython-36m-x86_64-linux-gn
u.so /usr/lib/python3/dist-packages/netifaces.so
vagrant#ubuntu18.04:~$ python3.8 -c 'import netifaces; print("works!")'
works!
Hint: I had to do the same for apt_pkg.so → apt_pkg.cpython-36m-x86_64-linux-gnu.so
I'm running VPS with ubuntu:
Distributor ID: Ubuntu
Description: Ubuntu 14.04.5 LTS
Release: 14.04
Codename: trusty
I tried to upgrade Python to 2.7.16 from 2.7.6.
Just after upgrade Gunicorn library failed to start with
Traceback (most recent call last):
File "/home/user/bin/python-2.7/bin/gunicorn", line 7, in <module>
from gunicorn.app.wsgiapp import run
File "/home/user/bin/python-2.7/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 9, in <module>
from gunicorn.app.base import Application
File "/home/user/bin/python-2.7/lib/python2.7/site-packages/gunicorn/app/base.py", line 12, in <module>
from gunicorn import util
File "/home/user/bin/python-2.7/lib/python2.7/site-packages/gunicorn/util.py", line 12, in <module>
import pkg_resources
File "/home/user/bin/python-2.7/lib/python2.7/site-packages/pkg_resources/__init__.py", line 82, in <module>
__import__('pkg_resources.extern.packaging.requirements')
File "/home/user/bin/python-2.7/lib/python2.7/site-packages/pkg_resources/extern/__init__.py", line 61, in load_module
"distribution.".format(**locals())
ImportError: The 'packaging.requirements' package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution.
I was told that it could be some setuptools issue, so I renewed them with
python -m ensurepip
Looking in links: /tmp/tmp98U8zf
Requirement already satisfied: setuptools in ./bin/python-2.7/lib/python2.7/site-packages (41.1.0)
Requirement already satisfied: pip in ./bin/python-2.7/lib/python2.7/site-packages (19.2.2)
(no changes)
I tried to do
pip install packaging
(no changes)
or downgrade setuptools to 19.2 (as suggested in Python 2.7 The 'packaging' package is required; normally this is bundled with this package)
and got
File "/home/user/bin/python-2.7/lib/python2.7/site-packages/gunicorn/config.py", line 8, in <module>
import copy
File "/home/user/bin/python-2.7/lib/python2.7/copy.py", line 52, in <module>
import weakref
File "/home/user/bin/python-2.7/lib/python2.7/weakref.py", line 14, in <module>
from _weakref import (
ImportError: cannot import name _remove_dead_weakref
Is there any working way to upgrade to 2.7.16 python in my case?
UPD:
I upgraded python with it with
wget https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tgz
make clean && ./configure --enable-unicode=ucs4 --enable-shared --prefix=/home/user/bin/python-2.7/ --with-ensurepip=install && make && make install
pip install -r ~/django/django_projects/requirements.txt
pip freeze: https://www.pastiebin.com/5d592ea701503
Thanks.
Scenario
First: When trying to execute a Python script, the following error message appears:
ImportError: The 'packaging.requirements' package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution.
Second: When trying to check the version of setuptools with pip show setuptools (or when using any other pip command), the next error message appears:
ImportError: cannot import name _remove_dead_weakref
Reason
This is because the current Python installation is somehow screwed up. In my case the mess was caused by an upgrade from Ubuntu 16.04 to Ubuntu 18.04.
Solution
With Virtual Environment
In the best case you were already using a virtual environment (this was my case). The solution here would be to recreate/setup your venv again (step-by-step):
$ cd /path/to/your/venv
# remove your old venv
$ rm -rf ./*
# create a new one
$ /usr/bin/virtualenv . --python=YOUR-PYTHON-VERSION
# activate venv
$ source bin/activate
# verify the correct python version is installed
$ python --version
For example replace YOUR-PYTHON-VERSION with python2.7 or python3.7.
After recreating your venv the problem should be fixed and you should be able to use pip again.
Without Virtual Environment
I think the best way to fix the problem would be to completely uninstall all non-default Python versions (for example Ubuntu 18.04 comes with Python 3.6+) and then make a clean reinstall of all other Python versions needed.
Try upgrading pip by typing:
pip install --upgrade pip
I have an issue with Python on my Mac with High Sierra.
After running pip install pysal, I have a successful installation. However, the module is not found when I run in python:
> import pysal
Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named pysal
I do not face such errors when running the same command in python2. However, most of my python scripts call the interpreter at the beginning of the file with #!/usr/bin/env python, so I would prefer not having to rely on python2 (I don't know how to do it either).
I figured out that this is python's executable is not on the same location where pip installs the modules:
macbook-pro-3:~ ME$ which -a python
/usr/bin/python
macbook-pro-3:~ ME$ which -a pip
/usr/local/bin/pip
I tried to follow the solution proposed by #J0ANMM in Modules are installed using pip on OSX but not found when importing . It recommeds downloading the script get-pip.py and executing it with sudo /usr/bin/python get-pip.py. I tried, but it did not change anything (the previous which commands still yield the same thing).
How do I manage to make pip work with my version of python?