I work on Ubuntu Xenial (16.04) with python3, I also installed anaconda.
I installed python3-gammu (with apt install python3-gammu or/and pip install python3-gammu) to test send SMS.
Just run python3 console and
>>> import gammu
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'gammu'
import sys
print(sys.path)
only return anaconda paths !
If I run
sudo find -iname gammu
…
./usr/lib/python3/dist-packages/gam
…
so if I add this path:
>>> sys.path.append('/usr/lib/python3/dist-packages/')
>>> import gammu
and it works !
Could you clarify this library path issue?
just
export PYTHONPATH=$PYTHONPATH:/usr/lib/python3/dist-packages/
To keep it at next reboot, put this line in your ~/.bashrc :
# added by Anaconda3 4.2.0 installer
export PATH="/home/my_user_name/anaconda3/bin:$PATH"
export PYTHONPATH="/usr/lib/python3/dist-packages/:$PYTHONPATH"
to active new .bashrc, do not forget to run
source ~/.bashrc
When you are trying to import any package it will check sys.path, which contains all paths of packages. If it find the package you want to import it will import it.
sorry for bad english...
Why use sys.path.append(path) instead of sys.path.insert(1, path)?
You may get clarity after seeing this ?
Related
I'm working on Windows 10, using python3.8. I wanted to install the ffpyplayer module, and did so using the command:
python3 -m pip install --upgrade ffpyplayer
There seemed to be no issues. Using python in PowerShell and stating import ffpyplayer doesn't return anything, however, when I try and run `from ffpyplayer.player import MediaPlayer. It returns the following error
from ffpyplayer.player import MediaPlayer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File
"C:\Users\g\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\ffpyplayer\player\__init__.py", line 10, in <module>
from ffpyplayer.player.player import MediaPlayer
ImportError: DLL load failed while importing player: The specified module could not be found.
Why is it able to import in one instance and not the next? What is causing the import error in the second example?
ffpyplayer builds ffmpeg include path as follows:
_ffmpeg = join(sys.prefix, 'share', 'ffpyplayer', 'ffmpeg', 'bin')
sys.prefix resolves to Python installation directory, e.g. C:\Program Files\Python.
If ffpyplayer is installed using PIP not in CMD administrator mode, the package is installed into user local %AppData%\Python directory. This is the reason ffmpeg does not get resolved as expected.
Solution:
pip uninstall ffpyplayer
CMD (Administrator mode)
pip install ffpyplayer
I answered a similar question, but basically import ffpyplayer in your specfile and add ffpyplayer.dep_bins to your tree like so:
*[Tree(p) for p in (source1 + source2 + ffpyplayer.dep_bins)]
https://stackoverflow.com/a/70392062/16355112
So, I'm a bit new to python and have mastered javascript.
I know that javascript modules are saved in node_modules folder in a directory
I did: $ pip install pyautogui
but when I'm about to run my code, it says :
'Traceback (most recent call last):
File "bruh.py", line 1, in <module>
import pyautogui, time
ImportError: No module named pyautogui'
where do python modules get saved to? 'python_modules' folder?????
also, idk what to do, can anyone help me with this error?
To see the path where your modules are saved once you pip them.
You can open a python shell, import sys and print the sys.path variable. One of the path would end with site-packages, thats the path where modules are saved.
$ python3
>>> import sys
>>> print(sys.path)
[‘’, ‘/Users/uname/.local/lib/python3.6/site-packages/‘]
could be a number of things. do you have multiple versions of python on your machine? If so you'd need to pip install to the correct python version, e.g.:
pip3 install pyautogui vs pip install pyautogui
When I try running virtualenv I get the following error:
[jelly#laptop Getskilled]$ virtualenv venv
Traceback (most recent call last):
File "/usr/bin/virtualenv", line 6, in <module>
from virtualenv import main
ImportError: cannot import name 'main' from 'virtualenv' (/home/jelly/.local/lib/python3.8/site-packages/virtualenv/__init__.py)
Virtualenv was working when I last used it for a project so I am guessing that an update cause it to break. I have tried reinstalling virtualenv and pip.
The closest post I could find was this one:
virtualenv: cannot import name 'main'
I tried following this post so I ran the following in the python interpreter:
import virtualenv
virtualenv.__file__
Which returned: '/home/jelly/.local/lib/python3.8/site-packages/virtualenv/init.py'
However, there was no file /usr/local/bin/virtualenv.py and there is no virtualenv.py in the .local directory so that solution in that post won't work for me.
What can I try next?
Update:
I found virtualenv.py in /usr/bin/ and it seems like it is causing the problem but I'm not sure how to update it to work with the current version. I moved it then tried reinstalling virtualenv but that did not generate a new virtualenv.py so still not sure what's going on.
This happened to me when I installed the new Ubuntu 20.04 LTS. I renamed the existing virtualenv file to something else, and it started working again. Not exactly sure why, but it was advice from this answer: https://stackoverflow.com/a/32859811/2477292
sudo mv /usr/local/bin/virtualenv /usr/local/bin/xvirtualenv
I added a symbolic link /usr/bin/virtualenv pointing to /home/jelly/.local/bin/virtualenv and it seems to be working now :)
The error can be resolved by modifying the pip file
Check the location of the file:
$ which pip
path -> /usr/bin/pip
Go to that location /usr/bin/pip and open a terminal.
Enter: $ sudo nano pip
You can see:
from pip import main
if __name__ == '__main__':
sys.exit(main())
Change to:
import sys
from pip import __main__
if __name__ == '__main__':
sys.exit(__main__._main())
then Ctrl + O write the changes and exit.
Hope this will do!!
Every time that I need to run my python program with:
python my_program.py
I get some error saying that some import was not found.
Some error like this:
Traceback (most recent call last):
File "graphic.py", line 1, in <module>
import matplotlib.pyplot as plt
ImportError: No module named 'matplotlib'
Than I run:
sudo python my_program.py
And every thing works just fine. How do I remove the sudo command to run my python codes?
ImportError: No module named 'matplotlib' happens when your Python doesn't find the module. sudo changes the enviornment variables; That's why.
To fix this, locate where matplotlib is installed in your computer, and verify the folder is part of your sys.path.
import sys
sys.path
['C:\\Python27\\tests', ..., ...]
Then you've two options: insert that path from your script, i.e adding a line such import sys; sys.path.append(<folder>) or configure PYTHONPATH env variable under your user appending the folder to the path.
PYTHONPATH env variable gets loaded to sys.path on startup.
Best solution to me is a general workflow for all projects: use virtualenviroment]1.
sudo pip3 install virtualenv
virtualenv myenv
source mynenv/bin/activate
Then you should install your libraries again with pip and they will be installed in your virtualenviroment, isolated from everything else.
Here is a post about installing a module in python3. When I use brew install python, then it installs it for 2.7.
When I use the method suggested by dan, which aimed to install it directly in python3 (who i really thank), but which didn't work :
# Figure out the path to python3
PY3DIR=`dirname $(which python3)`
# And /then/ install with brew. That will have it use python3 to get its path
PATH=$PY3DIR:$PATH brew install mapnik
The installation was successful but in python2. so I get:
For non-homebrew Python, you need to amend your PYTHONPATH like so: export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
so i finally add the path manually in python3 :
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')
I get this error :
Traceback (most recent call last): File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/mapnik/__init__.py", line 69, in <module>
from _mapnik import * ImportError: dlopen(./_mapnik.so, 2): Symbol not found: _PyClass_Type Referenced from: ./_mapnik.so
Expected in: flat namespace in ./_mapnik.so
Please help, I have spent so many hours on this ...
Thanks!!!
The Mapnik python bindings depend on boost_python. And both need to use the same python. The problem is likely that homebrew is providing a bottle of boost which includes boost python built against python 2.7 and not python 3.x.