0

I am having trouble importing modules into my python projects. I just switched over to a 2020 Macbook Pro and think that this may have something to do with the problem since I never had problems before. This mac runs zhs as the default shell.

#!/usr/bin/python3

import time
import os
import selenium

This is the only code that I have written so far (I never used to need to specify that it was a python file with #!/usr/bin...)

I get this error message: ModuleNotFoundError: No module named 'selenium' and I am pretty sure that it isn't a problem with selenium since the same problem occurred when I tried to import yaml

When I enter a python shell through the terminal, I can import selenium with no error...

$ python3
Python 3.8.5 (default, Jul 21 2020, 10:48:26) 
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import selenium
>>> 

Both selenium and my new project are inside /usr/local/lib/python3.8/site-packages I also tried modifying the PYTHONPATH in zshrc which didn't seem to help.

I think it should be a quick fix, but I'm not used to how this shell works. Any help greatly appreciated!

  • `/usr/bin/python3` and `python3` might be different? What's the output of `type python3`? Try changing the shebang to `/usr/bin/env python3` – Phu Ngo Sep 24 '20 at 05:20
  • Your own project definitely should not be in `site-packages`. How exactly did you install Selenium there? – tripleee Sep 24 '20 at 07:41
  • @PhuNgo `type python3` results in `python3 is /usr/local/bin/python3` why do I need a shebang in the first place if I named the file `main.py`? – Matt Hyatt Sep 24 '20 at 15:39
  • @PhuNgo your solution is the only one that fixed my problem. – Matt Hyatt Sep 24 '20 at 16:18
  • @MattHyatt shebang is a way to tell the shell which command to run with the file, while filename is not (well except for things like xdg-open for Debian desktop...). Your inintial shebang `#!/usr/bin/python3` uses a different binary than the one you invoked using `python3`, thus the different behaviour. If you specify the shebang as `#/usr/local/bin/python3` it should also work, but `/usr/bin/env python3` is just a safer way to use the same resolved binary from default PATH, i.e. the same python3 you invoked manually – Phu Ngo Sep 25 '20 at 05:47
  • Please check out https://stackoverflow.com/questions/3009192/how-does-the-shebang-work for more information – Phu Ngo Sep 25 '20 at 05:49

2 Answers2

0

If you have pip installed you can install selenium like

pip install selenium

for python3 and based on your permissions you can try

sudo pip3 install selenium

Edit

Since you can import selenium with no error on the terminal, your IDE may be pointing to a different installation of Python than where Selenium is installed.

Check where the IDE is pointing and if it's not pointing to where

`python3 -c "import sys; print(sys.path)"` 

this is pointing, correct it to that path and you're good to go.

bahdotsh
  • 459
  • 3
  • 17
-1

If you're using an ide then while creating a project you should choose a virtual environment where you had installed that selenium

First you must check in which environment you had installed selenium.