'requests' imported module won't work on IDLE - python

(Solution given at the bottom)
I downloaded the module requests in the command prompt using 'pip install requests'. It downloads fine and works from the command prompt, but when trying to import from the IDLE PyCharm I get the error message:
Traceback (most recent call last):
File "C:/Users/asher/PycharmProjects/begginer_questions/decode_a_web_page.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
By copy-pasting the 'requests' file into the location:
C:/Users/asher/PycharmProjects/begginer_questions
along with the files contained in the file path:
C:/Users/asher/PycharmProjects/begginer_questions/venv/Lib/site-packages/pip-19.0.3-py3.7.egg/pip/_vendor
the code shown below finally gave no error messages.
However, this only seems like a temporary fix. Does anyone know of a permanent fix that could work or any reasons as to why this is happening? The two common problems which often cause this to occur is that multiple versions of Python are downloaded and there is already a file called requests.py, neither are the case for me. I also don't understand why I had to copy-paste the second bunch of files considering they're already contained in begginer_questions.
I'm on Windows 10 with Python 3.7.3
import requests
url = 'https://www.nytimes.com'
r = requests.get(url)
r_html = r.text
Solution:
After a long while I found the answer so for anyone struggling with this problem I hope this helps.
My confusion came from the fact that when a project is created with up to date pip and Python, a virtual environment (venv) is automatically created and is a primary place where the project will look for modules/packages. In the command prompt navigate to your project - in my case it would be:
> cd User\asher\PycharmProjects\begginer_questions
Then enter the following command the activate the virtual environment "venv" pre-made by Python or Pycharm (note this is different for Mac):
venv\Scripts\activate.bat
In this virtual environment enter the command:
pip install requests
The solution seems (and probably is) very obvious, but this should work because sys.path will always look for packages in this virtual environment instead of where ever it was installed when I tried before. Don't forget to deactivate your virtual environment in the command prompt when you're finished installing whatever packages you want by entering:
deactivate

Related

No module named 'selenium' (Python3)

I'm trying to import selenium from a python3 application. I've already installed it asi you can see in the first image.
I also configured the vs code with python3 (image 2).
Both if i try to run it from vs code console or with python3 it says the same error.
Exception has occurred: ModuleNotFoundError
No module named 'selenium'
File "/Users/admin/Documents/Bot/bot.py", line 1, in
from selenium import webdriver
I've tried several answers from StackOverflow but it didn't seem to work with me.
Note: i'm not using a virtual environment.
Since apparently i have a lot of python3 installed on my system i just created a virtual environment.
go to your program folder and type:
python3 -m venv virtual-env
this will create a copy of python3 inside your program folder (in /virtual-env/bin/)
then you need to activate it:
source /virtual-env/bin/activate
this will activate your virtual environment, so you can use your own copy of python3 located on your folder.
Once activated, your console will look like this:
(virtual-env) user:program admin$
thanks to everyone and sorry for the lateness

VS code cannot import local python modules

All of my project files in VS code suddenly gives an error saying that it cannot import modules (even tho the modules are local i.e same directory and they used to work pretty well before).
The code works fine in pycharm but not in VS code, any idea whats going on?
Code:
from backend.util.crypto_hash import crypto_hash
from backend.config import MINE_RATE
error:
env DEBUGPY_LAUNCHER_PORT=34625 /home/nikhil/python-blockchain/blockchain-env/bin/python /home/nikhil/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/lib/python/debugpy/no_wheels/debugpy/launcher /home/nikhil/python-blockchain/backend/app/__init__.py
Traceback (most recent call last):
File "/home/nikhil/python-blockchain/backend/app/__init__.py", line 2, in <module>
from backend.blockchain.blockchain import Blockchain
ModuleNotFoundError: No module named 'backend'
Close VS Code,
start it again, Go to File > open folder (open your project folder in vs code),
if it gives a prompt to select a existing virtual environment, select that.
Then you should be good to go.
More unrelated information:
I assume the issue here is that there's some problem with VS Code not recognizing the virtual environment properly. This has happened to me several times and I cannot point out why that happens. But the above solution is a quick fix and always works for me.
I'm not sure if this will resolve OP's (ten-month old) question, but I've been struggling with the same error using debugpy while trying to configure VSCode's debugger.
Step 4 of this resource resolved the issue for me. In particular, I was using the -m flag when running debugpy, but the module to be run was not in the current working directory. Once I had changed this, the debugger worked as expected. As an example, if the command was originally:
python -m debugpy --listen 0.0.0.0:5678 ./some/directory/my_script.py
then the following two commands would rectify it:
cd ./some/directory
python -m debugpy --listen 0.0.0.0:5678 ./my_script.py
After doing this, I no longer received the "No module found" error.
The main reason is that VSCode does not automatically configure environment variables for you, but PyCharm does. The simplest method is adding your module file to system path.
import sys
sys.path.append(module_file_path)
The better solution is to config your environmental path named PYTHONPATH, adding related module path to it, and after that you will no longer need import system path manually.
Hope it works!

Incude FreeCAD in system path for just one conda virtual environment

I want to be able to import FreeCAD into my python scripts, but only in one conda virtual environment. Is there a way to do this without adding FreeCAD to the path at the beginning of each file? I am using Pop!_OS, which should behave like Ubuntu here.
I already found that you can import FreeCAD, but the source I found did so by appending the FreeCAD library location at the beginning of the file: https://www.freecadweb.org/wiki/Embedding_FreeCAD. It looks like you could circumvent this problem by modifying your path variable, and I was able to do so on Windows in my workplace. I just want to do this only for a particular conda virtual environment.
Ideally,
import FreeCAD
will work in a special virtual environment, but not in others.
As mentioned, I got the import statement to work on Windows already by adding FreeCAD's directory to the PATH environment variable. It worked with the default python in command prompt, which should be the anaconda installation, so I think it works in all virtual environments. On Linux, though, I cannot import FreeCAD in python even when I use
PATH=$PATH:/usr/lib/freecad-python3/lib/
which I got from "locate FreeCAD.so" . I get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'FreeCAD'
It would be really nice to be able to run the same code on both operating systems, and have the PATH modification confined to one virtual environment.
Conda does not look for packages from the PATH environment. Check this answer for the details. But first check whether your package can be installed using pip or conda.
You might go to the virtualenv site packages dir and add the path to the freecad into easy_install.pth

python is getting global modules instead of local ones inside of virtualenv

This is my first time using virtualenv and MySQLdb and I'm getting a strange error. After I setup this virtualenv, I installed MySQLdb from within the virtualenv (with the ENV actually activated). MySQLdb is not installed globally. When I'm in my ENV folder, open a python terminal, and try to import the module I get the following:
import MySQLdb
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named MySQLdb
However, when I do the same thing from the site-packages folder
...ENV/lib/python2.7/site-packages
import MySQLdb
everything seems to work just fine.
Also, when I run
help ('modules')
from either directory, I get very slightly different lists (even though it's telling me I'm using the same python executable). It's like for some reason the same python executable is getting it's modules from different places and the only time it's getting the modules it's supposed to have is deep inside the ENV, in this case the site-packages folder. If I'm not in the site-packages folder, it seems to get a different list and I get no access to MySQLdb.
I'm using Mac OSX Mavericks, XCode is up to date to the best of my knowledge (I don't find I use it very much).
I used this to setup the virtualenv
virtualenv -p /usr/local/Cellar/python/2.7.8/bin/python ENV
which python gives me this both in ENV and in site-packages
...ENV/bin/python
Versions
virtualenv version 1.11.6
python 2.7.8 (not the default python)
UPDATE:
Installed sqlalchemy
(ENV)tims-mbp:ENV timbauer$ pip install sqlalchemy
And am getting the exact same affect. It seems like even though it's using the correct python when inside ENV, that version of python only is getting the correct module list when it's directly inside the site-packages folder. Otherwise it looks like it's pulling from the global list
Found the issue. Inside ENV/bin/pip there was a line that said this
#!/Users/timbauer/Desktop/ENV/bin/python2.7
That needed to say this
#!/Users/timbauer/Desktop/ENV/bin/python
Switching the above out and restarting terminal seems to have fixed everything. I had to delete the virtualenv and reinstall everything to get it to work, but I'd just barely started so this wasn't really that big of a problem.

"ImportError no module named gnuradio" when trying to execute ./uhd_fft

I have followed instructions out lined here:
http://forums.nuand.com/forums/viewtopic.php?f=9&t=2804
and installed GNU Radio from GIT repo (scroll down to the section that says "Building GNURADIO from GIT". I used the ./build-gnuradio.sh script to do this and it took a while, but it appeared to build successfully, as per the instructions.
I am running on Ubuntu 12.04 LTS.
When I attempt to run the "./uhd_fft" function I get the following error message:
Traceback (most recent call last):
File "./uhd_fft", line 23, in <module>
from gnuradio import gr, gru
ImportError: No module named gnuradio
I have Googled this error message and most of the forums claim there is a problem with the PYTHONPATH.
When I do
echo $PYTHONPATH
/usr/bin/python2.7
But when I check the python2.7 directory I do not see gnuradio. So I guess it makes sense I'm getting an import error when it tries to import gnuradio. But the bigger question is why?
I installed GNU Radio (per instructions from nuand forum) using ./build-gnuradio.sh script. So I should have it installed.
I would appreciate if it python / GNU Radio experts from the community could weigh in.
There is a section later in the instructions with this boldface label:
Now for some voodoo to get the new program installed into system libraries and python paths and executable path.
Those instructions lead you through modifying your PYTHONPATH to pick up the gnuradio module, among other things. If you have followed those instructions, you will have to start a new shell to see any effect, or execute the .sh file by hand, since profile scripts only run when a new shell starts up or when they're run manually.
One of the reason for this error is, when the default python is selected incorrectly. To see which python is selected type the following command in the Terminal:
"sudo port select python"
it will display all the python available on your mac.
like:
python26
python27-apple(Active)
python27
For Gnuradio you need python27,if other python is active like in the example above you can change it by the following command:
"sudo port select python python27"
Now RUN your python code,it should work.If python27 is already active you may consider changing the path manually as suggested by others
Try this once the PATH variables have been set.
ln -sf /usr/lib/x86_64-linux-gnu/libvolk.so.1.3.1 /usr/lib/x86_64-linux-gnu/libvolk.so.1.3
On OSX 10.12.3 (16D32) this worked for me:
export PATH=/opt/local/bin:$PATH
python
> import gnuradio
For me python at the prompt was using Mac's python instead of the one in
/opt/local/bin/python
The link you have provided also mentioned that you might need to add dist-packages and site-packages.
The following command solved the issue for me:
export export PYTHONPATH=/usr/local/lib/python3/dist-packages:/usr/local/lib/python3.6/dist-packages:\
/usr/local/lib/python3/site-packages:/usr/local/lib/python3.6/site-packages:$PYTHONPATH

Categories

Resources