Cannot Import Python Packages on Linux - python

I ran this in the terminal to install all the packages for a machine learning project.
sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
It says the packages are already their newest version. When I run import numpy from the interpreter prompt, it just runs without a flaw.
But when I say numpy.version it says "module 'scipy.version' from '/usr/lib/python2.7/dist-packages/scipy/version.pyc'" without showing the version name. Also, the package import fails when I try a script.
On further experimentation, I figured a script as simple as
# Check python
print('Hello world')
is giving this error from a .py file,
./Script.py: line 2: syntax error near unexpected token `'Hello world''
./Script.py: line 2: `print('Hello world')'
Note that the script and version.version commands run fine from the terminal interpreter prompt.
Is something wrong very deep down?
Python version 2.7.12.

In order to get to know the version in python,
import numpy
print numpy.__version__
numpy.version gives the path.

If you want to check the version of numpy, try this:
import numpy
numpy.version.version
As for the following question, please post more information.

Related

'No module named PIL' Visual Studio Code Error

I'm having a problem with PIL where vs code says there is no module named PIL when there is. If I run the file without vs code the module imports fine. In the vs code problem tab it says this:
import PIL could not be resolved from source. Pylance(reportmissingmodulesource)
I know the library is installed because if I do pip install pillow, it says requirement already satisfied.
Things I've tried to fix it: reinstalling python, uninstalling and reinstalling pillow, upgrading pip, installing the PIL library(pip install Pillow-PIL).
None of these things worked so I am out of ideas for things to try. Could someone help me with this?
This part is important:
If I run the file without vs code the module imports fine
If something like this happens, then you are not running the same python interpreter, because modules are always installed to specific installations of python that you have.
Do the following:
Add to your script the first two lines
import sys
print(sys.executable)
This will print the path to the python executable that is interpreting that script. If you now run this script with and without vs code, it should print two different python paths. Now you can install to the python interpreter that is being used by vs code specifically by typing
/path/to/python/used/by/vs/code/python -m pip install pillow
in case someone stil has this problem on Mac or it didn't work, I used the code "python3 -m pip install pillow" in my vs code in terminal below but I had different code "from PIL import ImageTk,Image"
If anyone stumbles upon this problem and can't figure out what's wrong:
the first thing to do is simply restarting the Visual Studio Code instance.
That worked for me after running pip3 install Pillow from the VSCode terminal.
How about this?
pip install Pillow
For reference: https://pypi.org/project/Pillow/

Tkinter unable to configure

I have this problem when I try to run an app that imports turtle:
Traceback (most recent call last):
File "/home/panos/Documents/python/challenge_60.py", line 1, in <module>
import turtle
File "/usr/local/lib/python3.9/turtle.py", line 107, in <module>
import tkinter as TK
File "/usr/local/lib/python3.9/tkinter/__init__.py", line 37, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
I have done the below commands:
sudo apt-get install python3-tk
pip3 install tkinter
pip3 install PythonTurtle
pip3 install Tcl
sudo apt-get install python-tk python3-tk tk-dev
sudo apt-get install python3-PythonTurtle
And, although it is installed, the error still exists. I haven't understand what does wiki says. Could you explain, with all the details, what do I have to do, because I am a newbie at Linux and commands in general?
This error is probably made, because you have installed the tkinter , for instance, on python 3.7, but you are running your code with an interpreter based on python 3.8.
Another reasons, like installing the library in a virtual environment and trying to run the code out of the venv, can also
lead to this error.
Make sure pip and python command are in the same location/path
to make sure they are from the same SDK.
This command indicates where python is installed on your machine:
(Unix Only)
which python
Windows users, use this snippet below to find path of the installed python :
import sys
locate_python = sys.exec_prefix
print(locate_python)
Check the pip location using the which command as well.
which pip
If pip and python are in the same path, you probably won't get the error anymore.
First you have to pay attention to a few things 1- All the packs must be updated and installed If you have downloaded the packs, update them, especially the pip module that is for installation 2- Note that the spelling of the words you use is correct 3 - Maybe the code you write does not support that module, you should also pay attention to this issue 4- Finally, there are some problems in your code. First, to download the Tkinter module, you have to write pip3 install tk or pip install tk. You should not write the word Tkinter yourself. Update the latest version and then try again. If it still does not work, turn on your Windows 10 (if not) and finally the last solution is to change your ide vs code. The best ide for this situation. Is And you can also help to install and import the library of online articles. If you use pycharm, enter the Windows command line and type pip3 install future or pip install future, and then the thinner will be imported for you, and then enter the following command: from future.moves import tkinter and you can find how to install each house in different ideas with a simple search

Having issue importing requests and I assume it's my python runtime not being set correctly

Probably stupidly I tried to install the latest version of Python, in this case using the download from python site, but after doing that I was then getting python still running on the previous version python-3.6. I'm on OSX and was using sublime.
So I have been trying to work out how to update it to use the newest version. I've followed; https://opensource.com/article/19/5/python-3-default-mac.
All of the responses to queries now point to the python-3.9.5 version. So that's great and my runtime is using that. However after installing the requests using pip install I get the following error when running.
import requests
ModuleNotFoundError: No module named 'requests'''
[path: /Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]
I stumbled upon Modules are installed using pip on OSX but not found when importing which I have been trying to work through.
I have been able to run the import command successfully in terminal, however it's intermittent as I've tried again and it's broken, so I'm lost. I'm running it something trying to run the python3.6 version, which after updating I followed these instructions to remove when I have uninstalled that from my mac https://www.macupdate.com/app/mac/5880/python/uninstall.
If there is any ideas, would love some help, mainly to try and tell me what that error message is telling me.
In particular, what does this mean?
[path: /Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.6/
I should clarify too; when I run 'pip list'
I see
requests 2.25.1
Assuming that you are not installing requests package properly, and assuming your python executable is named python:
python -m pip install requests
If however, your python executable is named something else instead, e.g. python3, replace python with that name:
python3 -m pip install requests

Python cannot import openpyxl

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.

no module named 'numpy' (windows 7, python 3.4.3, numpy-1.9.2)

Please only respond to this post if you use Python on Windows, rather than Mac or Linux.
Error message:
>>> import numpy
Traceback (most recent call last):
File "stdin", line 1, in <module>
ImportError: No module named 'numpy'
Questions:
Regarding windows 7, python 3.4.3 and numpy-1.9.2, are there any
conflicts that would prevent these from working together?
Into which directory (please write out the complete directory path
starting with "C:") should I extract the contents of the numpy zip
file?
What is the exact command that I need to type into python command
prompt in order to install numpy?
Thanks in advance for your assistance
I was also facing this issue where in I tried using
import numpy
But it has given me error "ImportError: No module named 'numpy'"
I installed numpy using "C:\user>python -mpip install numpy" and it was successfully installed.
However I was again getting the same error
Then I checked that the path where in numpy was intalled was not listed in
import sys
print(sys.path)
Then I appended my path wherein numpy was installed using ">>> sys.path.append(r"C:\Users\xxxx\AppData\Roaming\Python\Python38\site-packages").
The above command worked fine, still problem not resolved then I restarted my python session again.
Finally it worked..!!!!(As after every append we have to restart our python session).
1) No there isn't.
2) You can use pip. pip install numpy
If you don't have pip installed, install it, its the most often used way of installing python packages. Yes it is possible to do under windows.
3) Once you have the python command prompt, you already have python installed.
1) no
2) It doesn't actually matter as pip or the installer will figure that out for you. However, just so you know, pip or whatever will install numpy into your site-packages folder.
3) I recommend downloading the NumPy installer from SourceForge:
http://sourceforge.net/projects/numpy/files/NumPy/1.9.2/
Then you don't have to worry about having the right compiler installed. However, if you want to use pip, then you'll need to install the appropriate compiler. This is documented at the following locations:
Microsoft Visual C++ Compiler for Python 3.4
https://matthew-brett.github.io/pydagogue/python_msvc.html
I'm not even close to an expert on Python. That said, I like the notion of "keep it simple." I chose to install a Python distribution that already includes numpy. Specifially I installed Python(x,y). Everything seems to be working just fine right after install. I appreciate all those who commented on my question. Thanks

Categories