Posts: 18
Threads: 7
Joined: Jan 2019
Jan-07-2019, 04:55 PM
(This post was last modified: Jan-07-2019, 05:23 PM by IMuriel.)
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
Posts: 3,458
Threads: 101
Joined: Sep 2016
(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
Posts: 7,316
Threads: 123
Joined: Sep 2016
Jan-08-2019, 12:27 PM
(This post was last modified: Jan-08-2019, 12:27 PM by snippsat.)
(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.
Posts: 18
Threads: 7
Joined: Jan 2019
Jan-08-2019, 08:56 PM
(This post was last modified: Jan-08-2019, 09:01 PM by IMuriel.)
(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
|