Selenium works in Terminal but not the Python Shell - python

I am running python on a Mac (10.15.6). When I run this script from the terminal, it works fine
from selenium import webdriver
But starting it from the Python IDLE or VSCode, I get the message:
ModuleNotFoundError: No module named 'selenium'
I have no clue why it works from the terminal and not from the shell. It would be great if someone could help me out or point me in a direction where I might find the answer :)

It's seems like the Python interpreter you're using in the shell is not the same one used by Vscode. You can change the Python environment to match the one you're using in your Shell, or even install selenium in the Python interpreter currently used with:
python -m pip install -U selenium

Try typing which python in terminal and console, it will point out which python it is using. You can set the python again in VSCode using ⇧⌘P, Select interpreter then choose the one that's working in terminal.

Related

Error occurring while installing and importing pynput

So I am trying to install and import pynput in VSCode but its showing me an error every time I try to do it. I used VSCode's in-built terminal to install it using pip and typed the following :
pip install pynput but this error is shown : Fatal error in launcher: Unable to create process using '"c:\users\vicks\appdata\local\programs\python\python38-32\python.exe" "C:\Users\vicks\AppData\Local\Programs\Python\Python38-32\Scripts\pip.exe" install pynput': The system cannot find the file specified
After receiving the following error, I tried using CMD to install it but the same error is shown. I also tried using python pip install pynput and it shows Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases. even though I have python 3.9.7 and I have selected it as my interpreter in VSCode and I have IDLE(Python 64 bit) installed. How may I resolve the following error? Any help regarding the same is appreciated
Thanks in advance :)
There's no such thing as an in-built terminal in VS code. When you open a terminal in VS Code, it opens the default, which on Windows is usually equivalent to opening up CMD.
If you selected Python 3.9.7 as your default interpreter in VS Code, it does not mean that it will visible to your CMD / terminal. It just means that the VS Code IDE will refer to that instance of Python when launching the program from VS Code itself using the green button (or F5), and when scanning your code to point out missing packages, etc.
CMD will only automatically detect your Python if it's in your PATH environment variable. You should add the Python 3.9.7 base and Scripts path to this.
Also, it would be best if you could first uninstall conflicting versions (like your 3.8.x) of Python and remove them from PATH, assuming that this won't cause any problems for you. Perhaps keep a record all the installed packages in this old version of Python for future reference using pip freeze or pip list.
Check if c:\users\vicks\appdata\local\programs\python\python38-32\python.exe exists by typing cd c:\users\vicks\appdata\local\programs\python\python38-32

Selenium doesn't load in Jupyter, but works in Python3 in console and Sublime Text

I'm trying to load selenium in a Jupyter Notebook running Python 3. When I try to import Selenium I get a "ModuleNotFoundError: No module named 'selenium'" error. This doesn't happen when I run the same command from Python 3 in the Terminal or using Sublime Text.
I've tried a number of fixes, but can't seem to find a solution.
It turns out that Jupyter was running a different version of Python3 than my terminal. I found the discrepancy using which python3 in the terminal and
import sys
sys.executable
I then specified pip3 from the path given by the latter command and voila, it worked!

Importing python modules in vscode vs interactive shell

I am just learning python. I am trying to import modules in vscode, installing them using pip. When I try to import the module it says (Pic 1) I have already satisfied the requirements but then (Pic 2/3) it says module is not found when I go to run the program in the Jupyter interactive shell. (Pic 4) However when I run the same code in the python interactive shell it works fine. This seems to be happening for all modules not just the one in this example (pyperclip). It seems like vscode/Jupyter may be looking in a different place than the python interactive shell is. However I am stuck at this point and not sure what else to try.
Thank you for you time and any help you may be able to provide.
(Pic1) Installing module with pip
(Pic 2) Code I am trying to run
(Pic 3) Output from Jupyter shell
(Pic 4) Same code in python interactive shell
I think you need to install you package using anaconda prompt, because sometimes its happen when you are using different platforms. So try pip install pyperclip in Anaconda prompt, hope this will solve your problem, if not then watch this video completely which will give you an idea how these import error occurs and what are the solutions.

how do i select an interpreter on my IDE which is Python IDLE

How do I select an interpreter on my IDE which is Python IDLE? I can't find the options to do that.
I managed to install Tensorflow but it only works when I import it in the terminal, not in my current IDE
What I want: Make my current IDE use the Python.exe that has been provided when I installed Tensorflow on my computer
What I tried: Using PYCHARM, it works (like a charm!) but I can't do that stuff like import module then have " >>> " then issue my commands etc...
IDLE does NOT provide such functionality - it works through idlelib, a package from stdlib so it's executed using pythonw -m idlelib. To change interprenter in IDLE, call it using a different interprenter - "C:\path\to\your\python\interprenter\pythonw.exe" -m idlelib (make sure idlelib is installed for target interprenter).

how to use relative import within python spyder IDE

I got anaconda python and is using the spyder IDE. I'm trying to figure out how can I use relative import for with the run bottom or F5.
suppose I have pkg/A/foo1.py, pkg/A/foo2/py, and foo1.py has "from . import foo2", if I hit run it will report relative import error.
I know how to do it in the command line environment where I can type, e.g. "python -m pkg.A.foo1". How can I do this in spyder IDE?
Thanks
jq
If there is pkg/A/__init__.py file i.e., if pkg.A is a Python package then from . import foo2 is correct. It doesn't matter where you write your code in the spyder IDE, notepad, or emacs; the code is the same.
The remaining question is how you run Python scripts in spyder IDE.
Don’t directly run modules inside packages i.e., don't run python pkg/A/foo1.py. It leads to Python modules being available under different names. See Traps for the unwary. Run it as python -m pkg.A.foo1 from the project directory instead.
Configure the command that is run on F5 if spyder IDE allows it.

Categories

Resources