Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unable to import pymysql
#1
hi, im learning python, and im developing a simple app in a local enviorment,
so im trying to make a mysql connection, so i installed pymysql using pip getting a successfully message

but in my file hello.py when i try to import
import os

import pymysql
from flask import Flask,url_for,request,render_template,redirect,flash,make_response,session

import logging
from logging.handlers import RotatingFileHandler 
app=Flask(__name__)
,

im getting a error message,unable to import pymysql
but if i go to Lib/site-packages path, i can find pymysql folder
also if i import pymysql thorugh python console im not getting errors,
so not sure what is going on, hope you can help me out

python version: 3.7.1
mysql version : 8.0.13

hello, it was a Library problem, apparently the library did not install in C/python37/Lib/site-packages, it only was installed in my vev folder, so i just copied the libraries in the python37 path
Reply
#2
(Jan-07-2019, 04:55 PM)IMuriel Wrote: hello, it was a Library problem, apparently the library did not install in C/python37/Lib/site-packages, it only was installed in my vev folder, so i just copied the libraries in the python37 path
That means you're not using the virtual environment. You created it, installed packages into it, but you aren't actually running your script from inside it.

https://docs.python.org/3/library/venv.html Wrote:Once a virtual environment has been created, it can be “activated” using a script in the virtual environment’s binary directory. The invocation of the script is platform-specific (<venv> must be replaced by the path of the directory containing the virtual environment):
Platform
Shell Command to activate virtual environment
  • Posix
    • bash/zsh $ source <venv>/bin/activate
    • fish $ . <venv>/bin/activate.fish
    • csh/tcsh $ source <venv>/bin/activate.csh
  • Windows
    • cmd.exe C:\> <venv>\Scripts\activate.bat
    • PowerShell PS C:\> <venv>\Scripts\Activate.ps1
Reply
#3
(Jan-07-2019, 04:55 PM)IMuriel Wrote: it only was installed in my vev folder, so i just copied the libraries in the python37 path
Don't do that in the future use pip to install to your OS Python,it's normal to use virtual environment when doing web-development.
This create a isolated environment where all dependencies are in one place,
will make it easier if need to move code to a server also a host to share code with the world eg DigitalOcean,Herko... ect.

So with virtual environment you need to activate as pointet out bye @nilamo,
or point your editor to the path of python interpreter in virtual environment(also python.exe).

I quick demo of using venv.
# Make virtual environment
C:\code
λ python -m venv web_env

# Cd in
C:\code
λ cd web_env

# Activate,see (web_env)
C:\code\web_env
λ C:\code\web_env\Scripts\activate

# Check pip
(web_env) C:\code\web_env
λ pip -V
pip 10.0.1 from c:\code\web_env\lib\site-packages\pip (python 3.7)

# Install PyMySQL
(web_env) C:\code\web_env
λ pip install PyMySQL
Collecting PyMySQL
  Downloading https://files.pythonhosted.org/packages/ed/39/.../PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)
    100% |████████████████████████████████| 51kB 711kB/s
Installing collected packages: PyMySQL
Successfully installed PyMySQL-0.9.3

# Test that i work
(web_env) C:\code\web_env
λ python
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymysql.cursors
>>> exit()

(web_env) C:\code\web_env
λ
So at this point when Activate,it will always use dependencies inside virtual environment from command line.
If in a editor need to point to C:\code\my_env\Scripts\python.exe,
or there are editors that find virtual environment like eg VS Code,PyCharm.
Reply
#4
(Jan-07-2019, 05:44 PM)nilamo Wrote:
(Jan-07-2019, 04:55 PM)IMuriel Wrote: hello, it was a Library problem, apparently the library did not install in C/python37/Lib/site-packages, it only was installed in my vev folder, so i just copied the libraries in the python37 path
That means you're not using the virtual environment. You created it, installed packages into it, but you aren't actually running your script from inside it.

https://docs.python.org/3/library/venv.html Wrote:Once a virtual environment has been created, it can be “activated” using a script in the virtual environment’s binary directory. The invocation of the script is platform-specific (<venv> must be replaced by the path of the directory containing the virtual environment):
Platform
Shell Command to activate virtual environment
  • Posix
    • bash/zsh $ source <venv>/bin/activate
    • fish $ . <venv>/bin/activate.fish
    • csh/tcsh $ source <venv>/bin/activate.csh
  • Windows
    • cmd.exe C:\> <venv>\Scripts\activate.bat
    • PowerShell PS C:\> <venv>\Scripts\Activate.ps1

actually i did that, but maybe i forgot to activate it before, to install the packages? im not sure, but is working so far, thanks for the reply

(Jan-08-2019, 12:27 PM)snippsat Wrote:
(Jan-07-2019, 04:55 PM)IMuriel Wrote: it only was installed in my vev folder, so i just copied the libraries in the python37 path
Don't do that in the future use pip to install to your OS Python,it's normal to use virtual environment when doing web-development. This create a isolated environment where all dependencies are in one place, will make it easier if need to move code to a server also a host to share code with the world eg DigitalOcean,Herko... ect. So with virtual environment you need to activate as pointet out bye @nilamo, or point your editor to the path of python interpreter in virtual environment(also python.exe). I quick demo of using venv.
# Make virtual environment C:\code λ python -m venv web_env # Cd in C:\code λ cd web_env # Activate,see (web_env) C:\code\web_env λ C:\code\web_env\Scripts\activate # Check pip (web_env) C:\code\web_env λ pip -V pip 10.0.1 from c:\code\web_env\lib\site-packages\pip (python 3.7) # Install PyMySQL (web_env) C:\code\web_env λ pip install PyMySQL Collecting PyMySQL Downloading https://files.pythonhosted.org/packages/ed/39/.../PyMySQL-0.9.3-py2.py3-none-any.whl (47kB) 100% |████████████████████████████████| 51kB 711kB/s Installing collected packages: PyMySQL Successfully installed PyMySQL-0.9.3 # Test that i work (web_env) C:\code\web_env λ python Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import pymysql.cursors >>> exit() (web_env) C:\code\web_env λ
So at this point when Activate,it will always use dependencies inside virtual environment from command line. If in a editor need to point to C:\code\my_env\Scripts\python.exe, or there are editors that find virtual environment like eg VS Code,PyCharm.
hello, i think actually i did that, maybe i forgot to activate the venv when i installed the package?, but nvm now its working perfeclty so far, thanks for the reply anyway
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating tables with pymysql using Xampp newbie1 0 2,115 Jul-15-2020, 11:10 AM
Last Post: newbie1
  pymysql.err.InterfaceError nikos 1 3,038 Feb-24-2019, 02:07 PM
Last Post: nikos
  Issue with bottle-pymysql nikos 13 5,469 Feb-23-2019, 11:15 AM
Last Post: nikos
  Insert data to SQL through pymysql and flask iainstott 3 7,720 Oct-24-2017, 03:04 PM
Last Post: iainstott

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020