Basic Flask Issue w/ Importing - python

I'm following a Flask tutorial and am getting an import error. I have a file called run.py which contains:
from app import app
app.run(debug = True)
When I run ./run.py, I get:
Traceback (most recent call last):
File "./run.py", line 2, in <module>
from app import app
File "/Users/myName/Desktop/SquashScraper/app/__init__.py", line 1, in <module>
from flask import Flask
ImportError: cannot import name Flask
This seems similar to this issue: http://stackoverflow.com/questions/26960235/python3-cannot-import-name-flask
So I attempted the checked solution by running:
virtualenv -p /usr/bin/python3 my_py3_env
Unfortunately, I get:
The executable /usr/bin/python3 (from --python=/usr/bin/python3) does not exist
Any ideas what may be happening here?
Thanks for the help,
bclayman

If you want your virtual environment to be Python 3 but don't know the installation directory, use which python3. Use that directory in your virtualenv -p [directory] my_py3_env command to set up the Python 3 virtual environment.
I sounds like your pip is installing to your Python 2.X directory. If you're okay with that, you'll need to run the app either with python2 run.py, python2.X run.py where x is your installed version, or change the symlink of python in /usr/bin/python to your installation of Python 2.
This question has some more information.
Regardless of the version of Python that you wish to use, you will need to install Flask to that version of Python. See this question for that.

Related

Mac gcloud install ImportError: No module named __future__

When installing gcloud for mac I get this error when I run the install.sh command according to docs here:
Traceback (most recent call last):
File "/path_to_unzipped_file/google-cloud-sdk/bin/bootstrapping/install.py", line 8, in <module>
from __future__ import absolute_import
I poked through and echoed out some stuff in the install shell script. It is setting the environment variables correctly (pointing to my default python installation, pointing to the correct location of the gcloud SDK).
If I just enter the python interpreter (using the same default python that the install script points to when running install.py) I can import the module just fine:
>>> from __future__ import absolute_import
>>>
Only other information worth noting is my default python setup is a virtual environment that I create from python 2.7.15 installed through brew. The virtual environment python bin is first in my PATH so python and python2 and python2.7 all invoke the correct binary. I've had no other issues installing packages on this setup so far.
If I echo the final line of the install.sh script that calls the install.py script it shows /path_to_virtualenv/bin/python -S /path_to_unzipped_file/google-cloud-sdk/bin/bootstrapping/install.py which is the correct python. Or am I missing something?
The script uses the -S command-line switch, which disables loading the site module on start-up.
However, it is a custom dedicated site module installed in a virtualenv that makes a virtualenv work. As such, the -S switch and virtualenvs are incompatible, with -S set fundamental imports such as from __future__ break down entirely.
You can either remove the -S switch from the install.bat command or use a wrapper script to strip it from the command line as you call your real virtualenv Python.
I had the error below when trying to run gcloud commands.
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/gcloud.py", line 20, in <module>
from __future__ import absolute_import
ImportError: No module named __future__
If you have your virtualenv sourced automatically you can specify the environment variable CLOUDSDK_PYTHON i.e. set -x CLOUDSDK_PYTHON /usr/bin/python to not use the virtualenv python.
In google-cloud-sdk/install.sh go to last line, remove variable $CLOUDSDK_PYTHON_ARGS as below.
"$CLOUDSDK_PYTHON" $CLOUDSDK_PYTHON_ARGS "${CLOUDSDK_ROOT_DIR}/bin/bootstrapping/install.py" "$#"
"$CLOUDSDK_PYTHON" "${CLOUDSDK_ROOT_DIR}/bin/bootstrapping/install.py" "$#"

Import from my package recognized by PyCharm but not by command line

I've created the following package tree:
/main_package
/child_package
version.py
where version.py contains a single string variable (VERSION)
Inside my script in child package I'm importing version.py by the following line:
from main_package.version import VERSION
While I'm running the code from PyCharm everything works great, however when I'm running the code via the command line I'm getting the following error message:
C:\Users\usr\PycharmProjects\project\main_package\child_package>python script.py
Traceback (most recent call last):
File "script.py", line 2, in <module>
from main_package.version import VERSION
ModuleNotFoundError: No module named 'main_package'
I've found in the internet that I might need to add my package to the python path, however it doesn't seems to work for me
PyCharm sets the Python Path at the root of the project (by default). To mimic this in a quick'n'dirty fashion, you just need to do this once in your shell session before invoking python whatever:
set PYTHONPATH=C:\Users\usr\PycharmProjects\project
The pythonic way is to have a setup.py file to install your project in the system (check python Minimal Structure):
from setuptools import setup
setup(name='main_package',
version='0.1',
description='main package',
license='MIT',
packages=['main_package'],
zip_safe=False)
Then you install it as follow:
python setup.py install for global installation
OR
python setup.py develop for local installation in editable mode

made a shell to run script on startup, suddently it gives me an importerror

I followed this guide: guide to create a startupfile which excecutes a python file on startup.
in step 2 it says I have to test the startupfile I just created and suddently my script says:
Traceback (most recent call last):
File "Display.py", line 1, in <module>
import pyowm
ImportError: No module named pyowm
the python file works perfect if I run it directly.
what I allready tried: run pip again to see if the lib was okay
check the /usr/local/lib/python3.4/dist-packages folder to see if it was there and it is.
I think this is a python issue and not a RaspberryPi issue thats why I uploaded it here.
runned by:
sh launcher.sh
inside is:
#!/bin/sh
# launcher.sh
# navigate to home directory, then to this directory, then execute python script, then back home
cd /
cd /home/pi/arduino/Python/Main/Master
sudo python Display.py
cd /
Simple fix: define the version of python which will be used. It used python 2.7. Yet the lib was for 3.4.

Trouble running Flask application

I have created a virtual environment specifying python3
$ virtualenv -p /usr/bin/python3 flask_tut
$ source flask_tut/bin/activate
created a file named hello.py containing:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello, World!'
and, while in my working directory containing hello.py, enter:
(flask_tut) $ export FLASK_APP=hello.py
(flask_tut) $ python -m flask run
However, this yields errors that I am struggling with:
Traceback (most recent call last):
File "/home/matt/Envs/flask_tut/bin/flask", line 11, in <module>
sys.exit(main())
File "/home/matt/Envs/flask_tut/local/lib/python3.5/site-packages/flask/cli.py", line 478, in main
...
ImportError: cannot import name 'flask'
This is weird to me, I have installed flask as pip list indicates I have Flask (0.11.1). Others have solved similar issues by using python 3 which I am using as python --version shows I am using Python 3.5.1+
If anyone can provide help that would be excellent.
I am using Linux Mint if that makes a difference.
Edit:
After trying 100 different things, I just deleted my virtual environment, and hello.py, created a new virtual environment, working directory and script, and there are no problems. I will post if I get down to the root issue as I suspect this may happen to others running through the Flask tutorial.

ImportError when using web.py with new environment

I just recreated all my python environment, reinstalled python and setuptools, and installed virtualenv.
I started a test enviroment with virtualenv --no-site-packages test, activated it with Scripts\activate.bat and then easy_install web.py.
Then I create a code.py file:
import web
urls = (
'/.*', 'index',
)
app = web.application(urls, globals())
class index:
def GET(self):
return 'ok'
if __name__ == "__main__": app.run()
And I get the following error:
File "...\code.py", line 1, in <module>
import web
ImportError: No module named web
But if I use the interactive shell it works:
>>> import web
>>>
Everything done in the same cmd with the enviroment activated.
Does anyone know what is going on?
Edit:
It happens for every package installed within the environment. First it was web.py, now BeautifulSoup (same issue, cant find module, but import works in python shell)
Edit2:
The activate script is not setting the new python executable and the pythonpath print sys.executable gives C:\Python27\python.exe.
Solved.
Windows was configured to open .py files with C:\Python27\python.exe. I can even remember setting this mannualy some time ago so I wouldn't have to use python to run files (oh lazyness, what have you done to me?).
That's why it was working with the interactive shell, but not by executing the code.py file.
Running the file using python code.py works perfectly.

Categories