Python3 root sudo venv - linux

I am running Python3.5 on Ubuntu via SSH and I have some errors there. I don't get why. If I run the following commands I get the respective errors:
(venv) root#servername: python3 __init__.py
File "__init__.py", line 1, in <module> import flask
ImportError: No module named 'flask'
If I run it with sudo like this, I get another Error:
(venv) root#servername: sudo python3 __init__.py
File "__init__.py", line 2, in <module> from .content_management import Content
SystemError: Parent module '' not loaded, cannot perform relative import
AND if I run it with Firefox, cause its a Flask App, the website works and shows NO ERRORS! Whats going on here??? I am going crazy with this!!

Seems that you didn't install Flask module on the machine where you run your python script. That produces the ImportError you get.
Install the Flask module e.g. using pip:
$ pip install Flask
After you've done so, Python should be able to load the module.

Related

can't make a python project into an executable because path issues?

I'm trying to turn my python project into .exe
I used : pyinstaller --onefile main.py
Then I moved the main.exe into the original file to be sure it can access all the dependencies.
When I run the main.exe, I get an error, failed to execute script main.
I typed cmd in the directory of the python project, and tried python main.py
It gave me this error :
Traceback (most recent call last):
File "main.py", line 3, in <module>
from PyQt5 import QtWidgets, QtCore
ModuleNotFoundError: No module named 'PyQt5'
Even though every module are installed, why is it telling me that?
Thank you.
You haven't installed pyqt5.
Try pip install pyqt5 or pip3 install pyqt5
for linux
sudo apt install python3-pyqt5

Installed the flask_sqlalchemy module but still got the same Error ModuleNotFoundError: No module named 'flask_sqlalchemy'

I am working on code from CS50 Beyond... online course available on YouTube By Prof.Brian Yu
This code is from Lecture ORM and API in models.py file.
I don't understand this error
(venv) (base) pglab#pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ export FLASK_APP=application.py
(venv) (base) pglab#pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ python3.7 application.py
(venv) (base) pglab#pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ flask run
* Serving Flask app "application.py"
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Error: While importing "application", an ImportError was raised:
Traceback (most recent call last):
File "/home/pglab/venv/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/home/pglab/Downloads/Compressed/orms_and_apis/airline2/application.py", line 2, in <module>
from models import *
File "/home/pglab/Downloads/Compressed/orms_and_apis/airline2/models.py", line 4, in <module>
from flask_sqlalchemy import SQLAlchemy
ModuleNotFoundError: No module named 'flask_sqlalchemy'
I check all the settings also,
(venv) (base) pglab#pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ python3.7 -m flask --version
Python 3.7.4
Flask 1.1.1
Werkzeug 0.16.0
(venv) (base) pglab#pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ pip install Flask
Requirement already satisfied: Flask in /home/pglab/venv/lib/python3.7/site-packages (1.1.1)
(venv) (base) pglab#pglab15:~/Downloads/Compressed/orms_and_apis/airline2$ python3 -m pip install sqlalchemy
Requirement already satisfied: sqlalchemy in /home/pglab/venv/lib/python3.7/site-packages (1.3.13)
Everything seems fine! I run my flask app many times, but when i started ORM programs in flask things becomes messy....
Can somebody please help me out!
I guess you have installed the SQLalchemy outside the virtual environment.
please install it after activating the virtual environment then only the virtual environment is able to search the modules.
the main reason is the virtual environment scope is limited its not global
please follow below command
source/bin/activate //this will virtual enviroment
sudo pip install Flask-SQLAlchemy==2.4.1 // this will install the packge within your virtual envoronment
To the get the Flask wrapper for SQLAlchemy, do
python3 -m pip install flask-sqlalchemy
instead of
python3 -m pip install sqlalchemy
Or, since you already have SQLAlchemy installed, doing the former will suffice.

"No module named... "when running python from terminal

I've Python 3.6 Installed on my ubuntu. When i try to run my codes via terminal, python doesnt recognize the modules I've installed with pip3. Lets take flask as an example. In the terminal I can do:
python3
import flask
And I dont get any Import error.But when I've a python file in any location which contains:
import flask
And i run it via terminal:
sudo python file_name.py
I get the following error:
ImportError: No module named flask
Why python doesnt recognize the modules?
check if flask is installed properly:
try pip3 freeze or pip3 list from the location where you are trying to run and check if it has flask is in the list.
if it doesn't exists then reinstall and try.

No module named 'info' on fresh Python 3 installation

I did a fresh python3 installation on OSX via homebrew:
brew install python3
Then I created a virtual environment for my project and installed scipy and scikits.samplerate:
virtualenv -p /usr/local/bin/python3 pythen_env
pip install scipy
pip install scikits.samplerate
However, when I try to import a function from scikits.samplerate, I get the following error:
>>> from scikits.samplerate import resample
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/my/project/path/pythen_env/lib/python3.6/site-packages/scikits/samplerate/__init__.py", line 4, in <module>
from info import __doc__
ModuleNotFoundError: No module named 'info'
Info happens to be the first module from the package itself that is imported in __init__.py.
Strangely, the module info.py exists in /my/project/path/pythen_env/lib/python3.6/site-packages/scikits/samplerate/:
ls /my/project/path/pythen_env/lib/python3.6/site-packages/scikits/samplerate/
__init__.py setup.py tests __pycache__
info.py setuphelp.py version.py
The error also happens when I try the same without virtualenv, as well as for other packages. How could I start to debug this issue?
The problem seems to be that the package scikits.samplerate does not support Python 3.X (see issue). However, there is a fork which supports Python 3.X. You can install it via
$ pip install git+https://github.com/gregorias/samplerate.git
As always: people can make anything they like in repositories. I did not check which changes gregorias made.
the git version does support py3
https://github.com/cournape/samplerate
(merged the PR from #gregorias)
I should find the time and procedure to update pypi too...

Python3 module import fails on Travis ci

I have made a Python 3 script for testing a project of mine. The script has this structure:
main.py
myRequest.py
external/
requests/
__init__.py
many files of the library...
When I run python3 main.py the file myRequest.py is imported. Inside that file, I do import external.requests as reqs.
This works for me, and also passes on Travis CI
However, when I put the above files in the folder test of my project, the Travis CI job cannot find the module:
ImportError: No module named external.requests.
When I tried running the script in an online IDE (c9, Ubuntu 14.04, Python 3.4.0) it was able to import it.
At c9, I have tried doing from .external import requests as reqs but it raises a :
SystemError: Parent module '' not loaded, cannot perform relative import.
Adding an empty __init__.py file or running python3 -m main.py did nothing.
What should I do so that the import is successful at Travis CI?
I encountered the same issue, so I am posting here in hope to help somebody:
Quick Fix
The quick fix for me was to add this line export PYTHONPATH=$PYTHONPATH:$(pwd) in the .travis.yml:
before_install:
- "pip install -U pip"
- "export PYTHONPATH=$PYTHONPATH:$(pwd)"
Have a setup.py
Having a setup.py which should be the default option as it is the most elegant.
With that you would resolve your relative import issue, try one configured like:
from setuptools import setup, find_packages
setup(name='MyPythonProject',
version='0.0.1',
description='What it does',
author='',
author_email='',
url='',
packages=find_packages(),
)
And then add this line in .travis.yml
before_install:
- "pip install -U pip"
- "python setup.py install

Resources