I have had some issues with importing external modules into IDLE on chromebook.
For example I would like to start with termcolor. I was able to download termcolor through the linux shell using the command:
python3 -m pip install termcolor
It downloads into a folder:
./.local/lib/python3.7/site-packages
I would now like to use termcolor in IDLE.
I have not been able to successfully change the directory that termcolor gets downloaded into therefore it was difficult to access within IDLE. However, I was able to use the following command to access that directory:
import sys
sys.path.append(('./.local/lib/python3.7/site-packages'))
I was then able to import termcolor and use it a little. However, when I try to do something like:
print(termcolor.colored('hi'))
I get the result:
hi[0m
I would like to try and get everything sorted out since as I progress towards more advanced modules, I would like to not run into similar problems. I have though about getting a different laptop such as a mac to maybe simplify things since Chromebook may make things more difficult for beginners.
So my questions would be:
1) Can I add into IDLE pathbrowswer ./.local/lib/python3.7/site-packages?
2) How do I correct so I do not get hi[0m as a result?
Related
I am new to Python so this may be a stupid question, sorry in advance if it is so!
The paths Python searches for modules using Spyder 4.1.4.:
[.., 'D:\Users\Martin\Anaconda3\lib\site-packages', ..]
The paths Python searches for when used via command line in windows:
[.., 'D:\Users\Martin\Anaconda3\lib\site-packages', ..]
Thus, both include path to
'D:\Users\Martin\Anaconda3\lib\site-packages'
where I store my cv2 module installed. Yet, Spyder can import cv2 and to my surprise cmd cannot import cv2 with an error No module named 'cv2'. What am I missing please? Can anyone verify if it works for him in command line please? (I changed/edited the question again to cut unneccessary long talk, though the essence stayed the same, in hope someone could answer).
I solved it. If you have a similar problem, you can check if your case is similar to mine and follow the steps.
I use Spyder and everything works fine. However my goal is to use Python via cmd. This could not be done because Python via cmd could not find my modules.
What I did: After typing python in cmd, Python worked and output its version by default. However, I noticed the version was different than version running in Spyder - I had two Python installations (one of which I didn't know about). I found the installation cmd uses by import sys and print(sys.executable). I uninstalled that Python installation.
Now, typing python in cmd does not do anything, it is an unknown command. Then I followed this guide to add my Python installation to Path (using Windows) by:
My Computer > Properties > Advanced System Settings > Environment Variables >
in System variables, I found "Path", clicked Edit, New, then input my Python installation path D:\Users\Martin\Anaconda3 as well as D:\Users\Martin\Anaconda3\Scripts and D:\Users\Martin\Anaconda3\Library\bin. This solved it for me.
I am relatively new to programming and posting to Stack Overflow. Please forgive my ignorance.
I am attempting to use a third party module in a program of my own, however I can't work out how to access the module within my Python script.
Mac OS X Sierra 10.12.4
Python 3.6.1
Anaconda 4.3.1
Specifically, I would like to be able to access anki (https://github.com/dae/anki).
I initially tried the line:
from anki import Collection
That resulted in a 'ModuleNotFoundError'.
Next I tried:
conda install anki
which also didn't work and yielded a 'PackageNotFoundError'
After more searching, I decided to try:
import sys
sys.path.append('usr/share/anki')
from anki import Collection
However, this also results in a 'ModuleNotFoundError'
What do I need to do to be able to access this module?
I am sure that it is possible because I have come across several other programs which make use of it:
-https://eshapard.github.io/anki/open-the-anki-database-from-python.html
I recognize that the link above purports to offer a solution to exactly this problem, however the solution proffered isn't working for me. Thanks in advance.
You are telling about Anki for desktop computers, which IS application written in Python, but it is NOT an installable Python package.
So the commands like
conda install anki
or
pip install anki
have no meaning.
So the only way is to download full source code of this project, unzip the content of it (only) folder into your project (change you actual folder to it) and then you may do import commands.
I'm trying to install PyDrive [a wrapper library of the google drive api for python] and pip is giving me this error. It did the same thing when trying to install things like matplotlib or mega.py [a mega.nz api for python].
Here's the error:
Anyone got a clue what's going on?
Cheers
You could try renaming that pip.py to something else.
There is a library called pip somewhere on your system (and it may also be bundled within pip.exe). That is different from the "entry point" script that actually runs pip from the command line. When you run pip, it will try to import the library called pip. If there is a script called pip.py in the Scripts directory (representing the entry-point script, not the library), it may import that instead of the real library. If this is indeed the problem, renaming pip.py to something else will remove the name conflict and allow pip to properly import the library it needs.
I'm not sure how you wound up with pip.py in your Scripts directory in the first place. I don't think it should be there. My Python installation on Windows doesn't have it.
I'm using netbeans to write a simple python program which I need the requests module for, I've downloaded requests through terminal and it all seems to be fine there but netbeans can't seem to find it.
This is the error that it's throwing up:
import requests
ImportError: No module named requests
I've tried installing the requests library directly into the python folder but the folder won't let me paste anything into it.
There do seem to be answers on the netbeans forums but their server is down so won't let me on their website to my annoyance!
EDIT
I've tried to run python setup.py install as per other answers on the website but had no luck.
EDIT
have tried completely uninstalling python and requests to make sure it wasn't an installation error but still no luck.
This clearly looks like an error of installation of the request module to some other place than where your netbeans expects when running the code.
In your console run
which python
Check if this gives the same path as the one set in your netbeans. You can set your path by adding new platform using Tools > Python Platforms > New:
I would suggest that you learn bit more about sandboxed environments such as virtualenv. This article shows how you can use a virtualenv to install packages and use the same virtualenv for netbeans so that whatever packages you install in the virtualenv will be available in the netbeans for you to use. For this case, it could be requests.
In the end I gave up with requests, as I was using requests to get json data from an API I decided just to go back to the drawing board and start over rather than attempt to fix something that I couldn't work out. I am now using the urllib import and whilst this may not be the most efficient way it works which is the most important thing.
I am curious about running pip.
Everytime I ran pip in command shell in windows like that
c:\python27\script>pip install numpy
But, I wondered if I can run it in python idle.
import pip
pip.install("numpy")
Unfortunately, it is not working.
Still cannot comment so I added another answer. Pip has had several entrypoints in the past. And it's not recommended to call pip directly or in-process (if you still want to do it, "runpy" is kind of recommended):
import sys
import runpy
sys.argv=["pip", "install", "packagename"]
runpy.run_module("pip", run_name="__main__")
But this should also work:
try:
from pip._internal import main as _pip_main
except ImportError:
from pip import main as _pip_main
_pip_main(["install", "packagename"])
This question is, or should be, about how to run pip from a python program. IDLE is not directly relevant to this version of the quesiton.
To expand on J. J. Hakala's comment: a command-line such as pip install pillow is split on spaces to become sys.argv. When pip is run as a main module, it calls pip.main(sys.argv[1:]). If one imports pip, one may call pip.main(arg_line.split()), where arg_line is the part of the command line after pip.
Last September (2015) I experimented with using this unintended API from another python program and reported the initial results on tracker issue 23551. Discussion and further results followed.
The problem with executing multiple commands in one process is that some pip commands cache not only sys.path, which normally stays constant, but also the list of installed packages, which normally changes. Since pip is designed to run one command per process, and then exit, it never updates the cache. When pip.main is used to run multiple commands in one process, commands given after the caching may use a stale and no-longer-correct cache. For example, list after install shows how things were before the install.
A second problem for a program that wants to examine the output from pip is that it goes to stdout and stderr. I posted a program that captures these streams into program variables as part of running pip.
Using a subprocess call for each pip command, as suggested by L_Pav, though less efficient, solves both problems. The communicate method makes the output streams available. See the subprocess doc.
At moment there are no official way to do it, you could use pip.main but you current idle session will not 'see' this installed package.
There been a lot a discussion over how to add a "high level" programmatic API for pip, it's seems promising.
Actually, I think, you can use subprocess.Popen(apt-get numpy), not sure how to do it with PIP though.
If your on a Mac you should be able to do it like this:
Go to your IDLE.
Run help('modules').
Find the HTML module.
Run help('HTML')
There should pop up a file map, for example this/file/map/example/.
Go to the finder and do command+shift+g and paste the file map there. Please delete the last file, because then your gonna go to the modules files.
There are all the modules. If you want to add modules, download the files of the module and put them there.
I hope this helps you.