I am using pyzo to run my python scripts. However I felt the need to switch over to the Atom code editor. I can run my python scripts without any problem.
At one point I need to use the library matplotlib. In pyzo I would do:
import matplotlib.pyplot as plt
But it doesn't work in Atom
Error message:
Traceback (most recent call last):
File "C:\Users\ivanl\Desktop\python trade\matplotlib.py", line 1, in
import matplotlib.pyplot as plt
File "C:\Users\ivanl\Desktop\python trade\matplotlib.py", line 1, in
import matplotlib.pyplot as plt
ImportError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package
Where should I look for installing matplotlib? Why is it working on pyzo and not on atom?
From The Module Search Path.
When a module named spam is imported, the interpreter first searches
for a built-in module with that name. If not found, it then searches
for a file named spam.py in a list of directories given by the
variable sys.path. sys.path is initialized from these locations:
the directory containing the input script (or the current directory).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
the installation-dependent default.
Which means you should avoid naming your modules with the same name as standard-library or built-in module names.
So you should rename your script file instead of matplotlib.py.
I faced a error like below while trying to import Matplotlib from atom:
Traceback (most recent call last):
File "lanes.py", line 3, in
import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'.
All I did is go to my C:\Users*user_name*.atom directory in command prompt and type pip3 install matplotlib. Now it works fine.
So in command prompt you need to give something like below:
C:\Users\*user_name*\.atom>pip3 install matplotlib.
Hope it works.
Related
I am struggling to install and import python libraries into my programs. originally I was using pydev but I kept getting messages like:
Traceback (most recent call last): File "C:\Users\satur\eclipse-workspace\DMD experimental\Main.py", line 11, in <module> from matplotlib import pyplot as plot ModuleNotFoundError: No module named 'matplotlib'
I installed matplotlib using py -m pip install matplotlib which produced a few messages like this.
WARNING: The script f2py.exe is installed in 'C:\Users\satur\AppData\Local\Programs\Python\Python310\Scripts' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. WARNING: The scripts fonttools.exe, pyftmerge.exe, pyftsubset.exe and ttx.exe are installed in 'C:\Users\satur\AppData \Local\Programs\Python\Python310\Scripts' which is not on PATH.
I was unsure what this meant so I tried running my program again and got the module not found error again.
being unable to resolve the issue I moved over to spyder because it came with many of the libraries I wanted thus avoiding the issue. however, as soon as I tried to import and install libraries not included with spyder I encountered the module not found error and a similar warning about the path. I added the paths mentioned in the warning to the PATH in spyder now when I run my code get this error message:
runfile('C:/Users/satur/.spyder-py3/proper orthogonal decomposition/dynamic mode decomposition experimental driver.py', wdir='C:/Users/satur/.spyder-py3/proper orthogonal decomposition')
Traceback (most recent call last):
File "C:\Users\satur\AppData\Local\Programs\Spyder\pkgs\spyder_kernels\py3compat.py", line 356, in compat_exec
exec(code, globals, locals)
File "c:\users\satur\.spyder-py3\proper orthogonal decomposition\dynamic mode decomposition experimental driver.py", line 13, in <module>
import mat73
File "C:\Users\satur\AppData\Local\Programs\Python\Python310\Lib\site-packages\mat73\__init__.py", line 11, in <module>
import h5py
File "C:\Users\satur\AppData\Local\Programs\Python\Python310\Lib\site-packages\h5py\__init__.py", line 25, in <module>
from . import _errors
ImportError: cannot import name '_errors' from partially initialized module 'h5py' (most likely due to a circular import) (C:\Users\satur\AppData\Local\Programs\Python\Python310\Lib\site-packages\h5py\__init__.py)
I did some looking around and found this
https://github.com/spyder-ide/spyder/wiki/Working-with-packages-and-environments-in-Spyder#installing-packages-into-the-same-environment-as-spyder
I followed what instructions I understood
import sys; sys.executable
Out[50]: 'C:\\Users\\satur\\AppData\\Local\\Programs\\Spyder\\Python\\python.exe'
which is different from what I get when I run it in command prompt or in the pydev terminal both of which give me
C:\Users\satur\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.exe
when i go to C:\Users\satur\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\ I see: 3 versions of idel, 3 versions of pip, and 6 versions of python.
I am deeply confused. I want to install and import libraries into my python programs. I want to be able to run my programs in either (or for that matter any) ide. I seem to be hitting the very similar issues in both. every tutorial I have found online describes the steps to take to do this but not what they are or why I am doing them none of them seem to work and the more times I try to install things the more confused I get.
could someone please explain: how to install and import libraries into a python program. what python is doing when I install things and try to import them. and why both IDEs have similar but different errors.
I am trying to import a pypi module (thinkx 1.1.2) into spyder. It is installed on anaconda and showing up on conda list. I my python path folders is my anaconda folder. When I attempt to import thinkx into spyder I get :
import thinkx
Traceback (most recent call last):
File "", line 1, in
import thinkx
ImportError: No module named 'thinkx'
According to module README, thinkx does not expose package named thinkx.
It provides the following modules:
thinkbayes: Code for Think Bayes.
thinkstats2: Code for Think Stats, 2nd edition
thinkbayes2: Code for Think Bayes, 2nd edition, not yet published.
thinkdsp: Code for Think DSP
thinkplot: Plotting code used in all of the books, mostly wrapper functions for matplotlib.pyplot
Try:
import thinkbayes
I am new to Python and am having trouble loading numpy in Wing IDE. I can load the module and use it fine in the command line but not in Wing IDE. Below is what I am seeing:
code:
import numpy as np
a=np.arange(15)
result:
[evaluate numpy.py]
Traceback (most recent call last):
File "C:\Users[my name]\Documents\Python\practice\numpy.py", line 2, in 0
builtins.NameError: name 'arange' is not defined
I have also tried to use the help() command:
code:
help(np)
result:
Help on module numpy:
NAME
numpy
FILE
c:\users[my name]\documents\python\practice\numpy.py
It is probably due to your code being in a file named numpy.py If you do this then 'import numpy' may import your module and not numpy. This depends on what's on the Python Path and possibly current directory, which probably explains why it works outside of Wing.
I'm beginning to learn python,
but when I try to import modules from an ather file I get this error:
Traceback (most recent call last):
File "./test", line 4, in <module>
from multip import table
ImportError: No module named multip
The both files are in the same directory
when I import modules like 'math' or 'os' it's work, the probleme is between files
OS:ubuntu 12.04
python version:python 3.2.3
You can import only files that have a .py extension. (or directories having a __init__.py file in them).
EDIT : I was not aware that modifying the PYTHONPATH environnement was considered as a bad practice. The reason given by #wRAR is that it has a permanent effect that can have uncontrolled side effects. You had better trying the first proposition (sys.path.append) to see if it can solve your problem. More about the sys.path.append vs PYTHONPATH can be found in this topic : PYTHONPATH vs. sys.path
Isn't it related to your PYTHONPATH environnement variable ? If you add '.' or the directory were you are working, I guess it should be ok
in your shell :
export PYTHONPATH=.:$PYTHONPATH
python test.py
or (for test purpose, not to be used systematically) in your python file :
import sys
sys.path.append(".")
I'm a Python newbie, so bear with me :)
I created a file called test.py with the contents as follows:
test.py
import sys
print sys.platform
print 2 ** 100
I then ran import test.py file in the interpreter to follow an example in my book.
When I do this, I get the output with the import error on the end.
win32
1267650600228229401496703205376
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named py
Why do I get this error and how do I fix it? Thanks!
Instead of:
import test.py
simply write:
import test
This assumes test.py is in the same directory as the file that imports it.
This strange-looking error is a result of how Python imports modules.
Python sees:
import test.py
Python thinks (simplified a bit):
import module test.
search for a test.py in the module search paths
execute test.py (where you get your output)
import 'test' as name into current namespace
import test.py
search for file test/py.py
throw ImportError (no module named 'py') found.
Because python allows dotted module names, it just thinks you have a submodule named py within the test module, and tried to find that. It has no idea you're attempting to import a file.
You don't specify the extension when importing. Just do:
import test
As others have mentioned, you don't need to put the file extension in your import statement. Recommended reading is the Modules section of the Python Tutorial.
For a little more background into the error, the interpreter thinks you're trying to import a module named py from inside the test package, since the dot indicates encapsulation. Because no such module exists (and test isn't even a package!), it raises that error.
As indicated in the more in-depth documentation on the import statement it still executes all the statements in the test module before attempting to import the py module, which is why you get the values printed out.