For some reason pip packages that I install in a venv or globally in my system are not able to be accessed from within files to run code.
Overview of my system: Macbook Pro 13-inch, M1, 2020
From the cli I run which python3 and get /usr/local/bin/python3
So now let's install Flask and create a Flask app.
pip3 install Flask
So now I open a file app.py in Vs Code or with nano in a Mac system terminal and make a basic Flask app.
In VS Code it says "flask" could not be resolved Pylance
Well that's obviously not good but for demonstration purposes let's finish a basic Flask
app:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
But let's try to run the app. I run python3 app.py
But here's what I get from VS Code:
File "/Users/matt/Projects/practice/server/app.py", line 1, in <module>
from flask import Flask
Here's the mac terminal:
File "/Users/matt/Desktop/test/app.py", line 1
import flask from Flask
Since I mentioned the venv before let's see what the venv is telling us.
Fire we create it:
python3 -m venv .venv
Then we activate:
source .venv/bin/activate
It shows Py server in the bottom right of my console, so we know it's active.
Inside we install Flask:
pip3 install Flask
So now in the directory we run which Flask and get /Users/matt/Projects/practice/server/.venv/bin/flask
So my cli says that Flask is there.
But if we try to import flask from Flask and run the code we get this error:
Import "flask" could not be resolvedPylance
Obviously there is something wrong going on where because when I run commands packages are able to be seen as installed, but then when you try to write code, the installed packages cannot be resolved by Pylance.
Please let me know if anyone knows if this is an issue with my M1 Mac or if there is a better way to check and reset the path so that things can be accessed.
This is actually a VS Code issue. Pylance is a language server that's used by VS Code.
You showed that you can find the installed package inside your virtual environment. You can verify it by running source .venv/bin/activate, then python, and in the interactive prompt importing flask: from flask import Flask. This should run smoothly.
Now all that's left is to point Pylance to this virtual environment: https://code.visualstudio.com/docs/python/environments#_work-with-python-interpreters
This is essentially the opposite of this problem: Import module works in terminal but not in IDLE
If I start a python session and try to import flask, then it works just fine. Despite that, if I just run flask from the terminal, it is not recognized.
I have confirmed that the locations which pip installs to is on my system path. (I'm on Windows 10 and I typed $Env:Path into Powershell to see that, for example, C:\Users\Zack\AppData\Roaming\Python\Python37\site-packages is in the system path.)
I am also sure that I did pip install --user flask not inside a virtual environment. When I run import flask; print(flask.__file__ I see C:\Users\Zack\AppData\Roaming\Python\Python37\site-packages)
Any other ideas for what could be going wrong?
Edit: I'm trying to run flask run. To check the path python uses, I did (in python) import sys; for i in sys.path: print(i)
As Python language based on Python interpreter, before you use Python, it is necessarily to activate the Python env.
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.
Currently i have a virtualenv created with the virtualenvwrapper. In that virtualenv i installed the cx_Oracle extension with pip install cx_Oracle.
I have a python script using several commands from cx_Oracle like connect and such.
When running my script thought the activated env (python script.py) it works fine and produces no errors.
But when i try to run the same script in PyCharm 4 it does not work. I have the virtualenv as intrepeter selected. When running the script i get an error as follows:
/Users/pgerrits/.virtualenvs/siebelaudit/bin/python3.4 -u /Applications/PyCharm.app/Contents/helpers/pydev/pydev_run_in_console.py 64420 64421 /Users/pgerrits/PycharmProjects/SiebelAudit/Audit/Siebel Audit/scratchpad.py
Running /Users/pgerrits/PycharmProjects/SiebelAudit/Audit/Siebel Audit/scratchpad.py
PyDev console: starting.
ImportError: dlopen(/Users/pgerrits/.virtualenvs/siebelaudit/lib/python3.4/site-packages/cx_Oracle.so, 2): Library not loaded: /ade/b/3071542110/oracle/rdbms/lib/libclntsh.dylib.11.1
Referenced from: /Users/pgerrits/.virtualenvs/siebelaudit/lib/python3.4/site-packages/cx_Oracle.so
Reason: image not found
When running the same script with the same command in the terminal with the env activated, i get no error.
I already tried the following:
- Added ENV variables for oracle_home, etc using a script
- added env variables using the pycharm env variables option
It is really annoying that i have to switch to my mac terminal for running and debugging. Has anyone a clue what could be the issue here?
I had to set the environment variables for ORACLE_HOME
DYLD_LIBRARY_PATH and LD_LIBRARY_PATH
and restart PyCharm to get cx_Oracle to work.
This is a known issue of PyCharm. The only way is to create virtualenv using PyCharm. If you create with virtualenvwrpper, there's a chance that PyCharm won't recognize it.
import os
import platform
if platform.system() == 'Darwin':
os.environ["ORACLE_HOME"] = '/opt/oracle/instantclient_11_2'
os.environ["DYLD_LIBRARY_PATH"] = '/opt/oracle/instantclient_11_2'
os.environ["LD_LIBRARY_PATH"] = '/opt/oracle/instantclient_11_2'
I have a project to do for after create a webpage that display the latest weather from my CSV file.
I would like some details how to do it (don't really get the http://flask.pocoo.org/docs/installation/#installation installation setup)
Can anyone mind explain me how to do it simply?
Thanks.
I'm running on Windows 7, with the Windows Powershell.
Install pip as described here: How do I install pip on Windows?
Then do
pip install flask
That installation tutorial is a bit misleading, it refers to actually running it in a production environment.
First install flask using pip,
pip install Flask
* If pip is not installed then install pip
Then copy below program (hello.py)
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
Now, run the program
python hello.py
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Just copy paste the above address line in your browser.
Reference: http://flask.pocoo.org/
Assuming you are a PyCharm User, its pretty easy to install Flask
This will help users without shell pip access also.
Open Settings(Ctrl+Alt+s) >>
Goto Project Interpreter>>
Double click pip>> Search for flask
Select and click Install Package ( Check Install to site users if intending to use Flask for this project alone
Done!!!
Cases in which flask is not shown in pip:
Open Manage Repository>>
Add(+) >> Add this following url
https://www.palletsprojects.com/p/flask/
Now back to pip, it will show related packages of flask,
select flask>>
install package>>
Voila!!!
https://www.youtube.com/watch?v=QjtW-wnXlUY&t=38s
Follow as in the url
This is how i do :
1) create an app.py in Sublime Text or Pycharm, or whatever the ide, and in that app.py have this code
from flask import Flask
app = Flask(__name__)
#app.route('/')
def helloWorld():
return'<h1>Hello!</h1>'
This is a very basic program to printout a hello , to test flask is working.I would advise to create app.py in a new folder, then locate where the folder is on command prompt
enter image description here
type in these line of codes on command prompt
>py -m venv env
>env\Scripts\activate
>pip install flask
Then
>set FLASK_APP=app.py
>flask run
Then press enter all will work
The name of my file is app.py, give the relevant name as per your file in code line
set FLASK_APP=app.py
Also if your python path is not set, in windows python is in AppData folder its hidden, so first have to view it and set the correct path under environment variables. This is how you reach environment variables
Control panel ->> system and security ->> system ->> advanced system setting
Then in system properties you get environment variables
On Windows, installation of easy_install is a little bit trickier, but still quite easy. The easiest way to do it is to download the distribute_setup.py file and run it. The easiest way to run the file is to open your downloads folder and double-click on the file.
Next, add the easy_install command and other Python scripts to the command search path, by adding your Python installation’s Scripts folder to the PATH environment variable. To do that, right-click on the “Computer” icon on the Desktop or in the Start menu, and choose “Properties”. Then click on “Advanced System settings” (in Windows XP, click on the “Advanced” tab instead). Then click on the “Environment variables” button. Finally, double-click on the “Path” variable in the “System variables” section, and add the path of your Python interpreter’s Scripts folder. Be sure to delimit it from existing values with a semicolon. Assuming you are using Python 2.7 on the default path, add the following value:
;C:\Python27\Scripts
And you are done! To check that it worked, open the Command Prompt and execute easy_install. If you have User Account Control enabled on Windows Vista or Windows 7, it should prompt you for administrator privileges.
Now that you have easy_install, you can use it to install pip:
easy_install pip
First: I assumed you already have Python 2.7 or 3.4 installed.
1: In the Control Panel, open the System option (alternately, you can right-click on My Computer and select Properties). Select the “Advanced system settings” link.
In the System Properties dialog, click “Environment Variables”.
In the Environment Variables dialog, click the New button underneath the “System variables” section.
if someone is there that above is not working, then kindly append to your PATH with the C:\Python27 then it should surely work. C:\Python27\Scripts
Run this command (Windows cmd terminal): pip install virtualenv
If you already have pip, you can upgrade them by running:
pip install --upgrade pip setuptools
Create your project. Then, run virtualenv flask
heres a step by step procedure (assuming you've already installed python):
first install chocolatey:
open terminal (Run as Administrator) and type in the command line:
C:/> #powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin
it will take some time to get chocolatey installed on your machine. sit back n relax...
now install pip. type in terminal
cinst easy.install pip
now type in terminal:
pip install flask
YOU'RE DONE !!!
Tested on Win 8.1 with Python 2.7
I have windows 10 and pythonv3.5. #uku answer is correct. however, problem I was facing is that where are python scripts which are to be added in environment variable. So I found out that we need to add
C:\Users\\AppData\Local\Programs\Python\Python35\Scripts
above location as environment variable. If it still doesnot work search for python in C Drive then find out script locations.
If You are using windows then go to python installation path like.
D:\Python37\Scripts>pip install Flask
it take some movement to download the package.
you are a PyCharm User, its good easy to install Flask
First open the pycharm press
Open Settings(Ctrl+Alt+s)
Goto Project Interpreter
Double click pip>>
search bar (top of page) you search the flask and click install package
such Cases in which flask is not shown in pip: Open Manage Repository>> Add(+) >> Add this following url
https://www.palletsprojects.com/p/flask/
Now back to pip, it will show related packages of flask,
select flask>>
install package