How to run GitHub Code quickly when your environment missing modules? - python

I would like to run the following project:
https://github.com/pyl0ne/flaskSaaS
At the 2nd step of executing causes the error
python manage.py initdb (if I run it like this it has no problem, but he have run it with python3 with no problem as well: https://youtu.be/NzmoPqte4V4?t=1623 )
I Do want to run it with python 3 so instead I use:
python3 manage.py initdb
To run it in python3 I have corrected in the code manage file:
ORIGINAL (tried with this one before): from flask.ext.script import Manager, prompt_bool, Shell, Server
Mine: from flask_script import Manager, prompt_bool, Shell, Server
based on: importerror: no module named flask.ext.script
I have tried to run it in PyCharm directly and checked the DE's interpreter all set to python 3.7 importerror: no module named flask.ext.script (It gives errors similar to the terminal)
Final Error:
Beli:flaskSaaS-master peterSimon$ python3 manage.py runserver
Traceback (most recent call last):
File "manage.py", line 1, in <module>
from flask_script import Manager, prompt_bool, Shell, Server #ORIGINAL: from flask.ext.script import Manager, prompt_bool, Shell, Server
ModuleNotFoundError: No module named 'flask_script'

You can get rid of confusion by using a virtual environment. So you only have one Python interpreter (version 3.x) and one pip version for this Python version:
git clone git#github.com:pyl0ne/flaskSaaS.git
cd flaskSaaS/
python3 -m venv venv
source venv/bin/activate
make install
python manage.py initdb
should work.

Check if make install actually installed flask_script.
If not, do pip install Flask-Script
and for python3
pip3 install Flask-Script

Related

Error while running Package in Command prompt , No module found

I have created a package named as patterns along with test.py,
test.py has the following code lines:
from patterns.shapes import *
square()
[Module Error]
I have copied the test.py file to the same location F:, where I trying to run via command prompt.
To gain access to this package from any location I have installed it using pip install . in the windows command prompt, which was successful, but when I try to access the file test.py I am getting the error as below :
F:\>py test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
from patterns.shapes import *
ModuleNotFoundError: No module named 'patterns'
My package folder is stored in a different drive.
While working on a python project, one must always work in a virtual environment. Please check if you have your virtual environment activated. If not, then follow the steps:
cd your_working_directory
virtualenv environment_name where environment_name can be any name you want.
environment_name/Scripts/activate if on Windows
Or environment_name/bin/activate if on MacOS
Then after activating the environment you can install all the dependencies using pip. i.e pip install patterns

Remove sudo to run python script

Every time that I need to run my python program with:
python my_program.py
I get some error saying that some import was not found.
Some error like this:
Traceback (most recent call last):
File "graphic.py", line 1, in <module>
import matplotlib.pyplot as plt
ImportError: No module named 'matplotlib'
Than I run:
sudo python my_program.py
And every thing works just fine. How do I remove the sudo command to run my python codes?
ImportError: No module named 'matplotlib' happens when your Python doesn't find the module. sudo changes the enviornment variables; That's why.
To fix this, locate where matplotlib is installed in your computer, and verify the folder is part of your sys.path.
import sys
sys.path
['C:\\Python27\\tests', ..., ...]
Then you've two options: insert that path from your script, i.e adding a line such import sys; sys.path.append(<folder>) or configure PYTHONPATH env variable under your user appending the folder to the path.
PYTHONPATH env variable gets loaded to sys.path on startup.
Best solution to me is a general workflow for all projects: use virtualenviroment]1.
sudo pip3 install virtualenv
virtualenv myenv
source mynenv/bin/activate
Then you should install your libraries again with pip and they will be installed in your virtualenviroment, isolated from everything else.

python virtualenv module import error

I am using virtualenv and have activated it and tested it like this:
source .virtualenvs/myapp/bin/activate
pip freeze
(myapp) me: redis==2.05
Then checking redis can import OK:
(myapp) me: python
>>>import redis
This works OK.
However on running the following
(myapp) me: sudo ./manage.py database create
I get the following error:
Traceback (most recent call last):
File "./manage.py", line 4, in <module>
from myapp import manager
File "/home/me/myapp/__init__.py", line 1, in <module>
import redis
ImportError: No module named redis
Redis is clearly installed for the virtualenv, any thoughts on what could be going wrong?
I suspect that sudo is the reason. It does not properly preserve the virtualenv.
Just try to create a shell script, which first sources the environment and than executes python. Than run this shell script with the sudo command:
#!/bin/sh
# ensure that working directory is right
source .virtualenvs/myapp/bin/activate
./manage.py database create
and run it:
$ chmod ugo+x my-startup-shell.sh
$ sudo my-startup-shell.sh
If that works, do some experiments with sudo like possibly passing the -E option to inherit the environment etc.

Python path issue with django

In centos 6.7 python 2.6 is pre installed. I installed django==1.7.0.
But while running server i am getting a issue:
python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 10, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
After that i installed python2.7. I just need to know is there any way so that i ll set the path of python version in manage.py
May be any lead to find out the solution.
anyone having any idea?
Thanks
Probably you are not using the correct python version where you installed django.
You can check your python versions on system with command:
ls /usr/local/lib | grep python
normaly if you don't have more than 1 instalation of python 2 and one of python3 pip installs on python2 and pip3 installs on python3
you can set the new virtualenv by setting the path of python you want to use
virtualenv -p /usr/bin/python3.4 <project_name>
If you are using Django 1.7 you probably want to use
python3 manage.py runserver

django wont show up in virtualenv

I am trying to use virtualenv.
I installed it on my ubuntu system followed simple steps given here
but when I activate my virtual environment and try to do
$ python manage.py runserver
it throws error
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
I tried to see if django is accessible from my virtual env so it tried to
$ python
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
and the same import django works fine out of the virtualenv.
I also tried to delete the django folder from root python setup and reinstalling it. but all in vain, the problem is still the same. What should I do now ?
PS: I tried to run easy_install Django & easy_install django gazillion times from the virtual env everytime it says django is already their.
PPS: I dont know if it is of importance but their was a django installation present prior doing all this in the system.
Check your manage.py, the first line needs to define the python executable, usually #!/usr/bin/env python. This should be the path to the python executable in your virtualenv and not to the global executable. If you had installed Django system wide this wouldn't be a problem.
Just in case
virtualenv venv
source venv/bin/activate
python manage.py runserver
[Adding this info for Windows users who hit the same problem]
If you're running into this problem on Windows then make sure that your virtual env is active (your prompt will be something like (venv) d:\myproject) and that you're running python.exe explicitly.
On the latter point, do not run py.exe (the Python Launcher introduced in 3.3) or rely on a .py file association to run Python. Both of those will run a version of Python outside of your virtual env and therefore will have the wrong site-packages.
If your virtual env has django 1.8.6 but your core Python installation does not have django, then:
python -c "import django;print(django.get_version())" will work and print 1.8.6
py -c "import django;print(django.get_version())" will fail with "ImportError: No module named django"

Categories