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!!
Related
For some reason when I install a package through Anaconda, it is not available in Spyder. When I execute the following command in anaconda and in spyder I get different files.
Anaconda:
import sys; sys.executable
'C:\\Users\\onp1ldy\\AppData\\Local\\conda\\conda\\envs\\deeplearning\\python.exe'
Spyder:
import sys; sys.executable
'C:\\Users\\onp1ldy\\AppData\\Local\\conda\\conda\\envs\\deeplearning\\pythonw.exe'
Can anyone help me with this? I am not sure what to do...
By Default: You can control which one of the executables will run your script. Such as, when opened from Explorer by choosing the right file_name like:
1. python.exe is a terminal-based (console) application to run and lunch CLI-Type Python-scripts.
*.py files are by default associated (invoked) with python.exe
2. pythonw.exe is a GUI-based app for lunching Graphical User interface-(No_UI_at_all_Scripts)
*.pyw files are by default associated (invoked) with pythonw.exe
TO SUMMARIZE AND COMPLEMENT MY OPINION:
First of all, Python-Binary that you're trying to run doesn't have Package installed. It does have a directory path named as package_name like torch at the search path of modules, and it is being treated as a Package namespace like for me:
torch.Tensor(5, 3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'torch' has no attribute 'Tensor'
For your current python binary, you need to install your package correctly. Visit Reference: Home-Page
python3.7 -m pip install http://download.pytorch.org/whl/cu80/torch-0.2.0.post3-cp35-cp35m-manylinux1_x86_64.whl
python3.7 -m pip install torchvision
More importantly, replace the pip or pip3 as the Home-Page instructions use with python3.7 -m pip;. In the end, don't forget to include python3.7 to be the full path to your Python binary.
Run this in spyder and see if you can access the package.
import subprocess
subprocess.call('pip install numpy', shell=True)
import numpy
based on your description the problem may be connected to the Python interpreter that you are using in Spyder. There is a similar issue on Stack Overflow at this url:
Issue on Anaconda and Spyder
You can try the solution proposed by Bremsstrahlung.
Hope this can help you.
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 ?
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.
I am trying to run python manage.py on a Django app and I am getting this error:
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
Brocks-Mac-mini:firstdjango BrockMorrison$
I have Django installed on my system. Using pip freeze | grep -i django I get the following:
Django==1.11.1
When I search for the path that Django is installed it gives me:
/usr/local/lib/python2.7/site-packages
I have tried updating to the latest version of Django and when I try running pip install django in the folder it tells me that the requirement is already satisfied. I have tried looking on other stack overflow posts, but none of the other solutions have helped me. Any help would be appreciated.
This error is most common when django isn't found in the current python environment.
I'm pretty sure the problem is with your PYTHONPATH not being properly set to use the local python lib you have. I've seen this before on osx. Try to fix with:
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
You should be able to just add that to your .bashrc or on osx you're probably using zsh so add to bottom of your .zshrc. But I really recommend setting up pyenv over this fix as it is a much cleaner way of dealing with multiple python environments and projects going forward.
So, the pyenv solution is to setup pyenv https://github.com/pyenv/pyenv, install python version you want, create a virtualenv with it, activate the virtualenv, pip install django.
I am running Windows 7 and using Python 2.7.
I have installed openpyxl using easy_install. It looks like the installation was successful. I changed the directory and fired up Python.
>>> import openpyxl
>>>
So, this should mean that Python is able to find openpyxl. However, when I execute a simple test program excell_tutorial1.py and run it, I get the following:
Traceback (most recent call last):
File "C:/Python27/playground/excell_tutorial1.py", line 7, in <module>
from openpyxl import Workbook
ImportError: No module named openpyxl
Very confusing! It could find it in prompt line but not in the program!
import os, sys
the_module ="C:\\Python27\\Lib\\site-packages\\openpyxl-2.3.3-py2.7.egg\\openpyxl"
if the_module not in sys.path:
sys.path.append(the_module)
if the_module in sys.path:
print sys.path.index(the_module)
print sys.path[18]
so, this gives me:
18
C:\Python27\Lib\site-packages\openpyxl-2.3.3-py2.7.egg\openpyxl
Anyone can think of what the problem might be?
Much appreciated
I had the same problem solved using instead of pip or easy install one of the following commands :
sudo apt-get install python-openpyxl
sudo apt-get install python3-openpyxl
The sudo command also works better for other packages.
While not quite what you ran into here (since you state that you are using python 2.7), for those who run into this issue and are using python 3, you may be unintentionally installing to python 2 instead. To force the install to python 3 (instead of 2) use pip3 instead.
See this thread for more info:
No module named 'openpyxl' - Python 3.4 - Ubuntu
Try deleting all openpyxl material from C:\Python27\Lib\site-packages\
Once you do that try reinstalling it using pip. (This what worked for me)
At times this can be a simple permission issue. As it was in my case. I installed it in my local directory with my login.
python ./setup.py install
but some of the other user were not able to import the module.
They were getting this error:
ImportError: No module named openpyxl
Hence I simply gave exe permission to 'others'
chmod -R 755
That solves the problem at least in my case.
Go to the directory where pip is installed, for eg.C:\Python27\Scripts and open cmd (simply type cmd in address bar ). Now run the command "pip install openpyxl". It will install the package itself. Hope this will solve your problem.
Try this:
!pip install openpyxl
I had the same issue on 3.8.2
I found out that python was installed in two locations on my machine (probably py and python, just a guess)
Here:
C:\Users<userAccount>\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8\LocalCache\local-packages\Python38
and Here:
C:\Python38
I deleted the one in my C drive and everything is working well now. I would double check to see where your packages are getting installed first, before deleting. Which ever one is being used, keep that one.
For this case, check to see where this package got installed:
C:\Users\<userAccount>\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8\LocalCache\local-packages\Python38\site-packages\openpyxl
keep that directory.
What worked for me was to open the terminal as an administrator, cd to the 'scripts' file of where python (different for each version) is stored, and then install using pip:
cd C:\Users\Salfa\AppData\Local\Programs\Python\Python39\Scripts
pip install openpyxl
This resolved the problem for me.