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
Related
I am unable to import any installed module in my python script "scrapper.py".
I followed these steps:
Inside scrapvenv folder created a virtualenv named scrapvenv
activated scrapvenv and pip install requests and pip install bs4
created a python file "Scrapper.py" inside the base scrapvenv folder
though the pip list shows that these module are installed But import doesn't work.
Vscode shows that the python interpreter is 3.9.4 64 bit
If you are using VSCode, and assuming that you are running your script from the terminal, make sure that VSCode has selected the correct interpreter from your virtual environment, by making sure that the command line contains your environment name in brackets, like so:
(scrapvenv) C:\...\scrapenv\>
If it does not, make sure to select it manually by pressing Ctrl+Shift+P, searching for Python: Select Interpreter, and choose from a list, or browse manually if you don't see your environment in the list
I created virtual environment and activate it.
installed packages but unable to import them from virtual environment.
pip freeze:
But getting error trying to import module:
Traceback (most recent call last):
File "z:\Documents\Python\Projects\ProjectName\tempCodeRunnerFile.py", line 1, in <module>
import paramiko
ModuleNotFoundError: No module named 'paramiko'
How to make sure .py file uses virtual environment?
Also if I run
import sys
print(sys.path)
Result:
'C:\\Users\\Username\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages'
So it does not uses virtual environment, is that correct?
In many cases it's not necessary to activate a virtual environment. Typically you could do something like the following from anywhere without activating the virtual environment:
C:\path\to\venv\Scripts\python.exe -m pip somecommand
C:\path\to\venv\Scripts\python.exe different\path\to\script.py
etc.
Additionally you could specify the absolute path to the python.exe as a she-bang at the top of your Python script, and execute the script directly (for example with a double-click) without calling Python explicitly.
#!/path/to/venv/bin/python3
print("Hello world")
References:
https://docs.python.org/3/using/windows.html#shebang-lines
https://docs.python.org/3/library/venv.html#creating-virtual-environments
You should invoke the python that is installed within your environment. That is
source myenv/bin/activate
python path/to/script.py
In Unix, you would add the following #!/usr/bin/env python at the top of your script and this would allow you to run it without specifying the python binary.
source myenv/bin/activate
path/to/script.py
Possibly that works in Windows in the same way or differently.
Some pip installs don't work on a computer, since recently I changed to Python 3.
for example, pywinauto
I used CMD:
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Scripts>pip install pywinauto
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Scripts>pip freeze
It shows:
pywinauto==0.6.8
But when I:
import pywinauto
It gives:
ModuleNotFoundError: No module named 'pywinauto'
I moved the 2 folders "pywinauto" and "pywinauto-0.6.8.dist-info" from:
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\
To:
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Scripts\
And:
import sys
sys.path.append('C:\\Users\\NAME\\AppData\\Local\\Programs\\Python\\Python37-32\\Scripts\\')
It still doesn't work. (it works for some other cases)
Windows 64 settings seem ok with: Environment Variables > User variables
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Scripts\
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages
Environment Variables > System variables
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Lib\
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages
p.s. I have Anaconda installed in the machine, and no Python 2. Not sure whether it causes the problem.
Go to your conda terminal, activate the virtual environment.
pip install <your package name>.
Now go to your editor where you're running this code. (like pycharm or Sublime)
select your python interpreter as venv (Anaconda). Usually this option is available in project setting.
Run your code, it should work now.
Seems the problem relates to Anaconda.
Problem solved by run CMD:
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Scripts>pip instsall pywinauto
Then manually moved the 2 folders "pywinauto" and "pywinauto-0.6.8.dist-info" from:
C:\Users\NAME\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\
To:
C:\Users\NAME\AppData\Local\Continuum\anaconda3
The package functions normally.
(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
I'm using a script to run Odoo11 via pycharm in ubuntu (openerp_openserver script)
When i try to run the program , it fails at some point , when it import pdf module and i have this error :
ImportError No Module Named 'PyPDF2' as you can see in this Image
I Already installed PyPDF2 via this command (i have python3.5 already installed) :
sudo apt-get install python3-pypdf2
So im wondering , what is the problem , why pycharm cannot find and import pypdf2?
Thanks
EDIT :
When i Try to import PyPDF2 using the Python command , i dont have error
Firstly you should try to check whether you can import PyPDF2 library from a python console.
Run from your native console:
python3 -c "import PyPDF2"
If no error message occurs, the problem is not in the library.
Check a path of python interpreter that is used by PyCharm.
Navigate inside PyCharm:
Ctrl-Alt-S > Build, Execution Deployment > Console > Python Console
The path should be to /usr/bin/folder.
If not - change it to a path of the desirable python interpreter that is inside /usr/bin/ folder.
I hope this helps!
Will be good if someone can add a way to solve this type of problem in Windows environment.
For windows you may try installing psycopg2 and requirements again for venv(virtualenvironment.
For psycopg2 install you should follow the steps under "Virtual Environment Installation Notes" title on http://www.stickpeople.com/projects/python/win-psycopg/