python virtualenv ImportError No module named inspect time flask - python

I recently deleted some old development folders off my disk, and now one of my virtualenv projects doesn't work. I noticed the problem when I tried to import flask.
I am using Ubuntu and I was using python2.7 in the virtualenv instead of python2.6 which is the default python.
I will describe how I fixed it, but I was wanting to know if there was a better way.
fyi I use bash in the terminal by default...
download source from http://www.python.org/download/releases/2.7/
move source file into /project/src/dir and extract
change the working directory of the terminal to the newly extracted directory
configure and make
mkdir python2.7
./configure --prefix=/project/src/dir/python2.7
make
make install
create virtualenv and specify python to use
virtualenv --no-site-packages -p /project/src/dir/python2.7/bin/python2.7 projectname
enter the virtual environment
cd projectname
source bin/activate
get flask if you want it
pip install flask
test to see if we can import time and inspect
(bash)
python
(python shell)
import time
import inspect
test to see if we can import flask
import flask

Perhaps you were using --system-site-packages before hand and flask was available via a path external to your VirtualEnv.
Keep in mind that older versions of VirtualEnv were not very relocatable either if they moved at all.

Related

Why does my virtualenv python3 work fine on my local machine but not when I upload the virtualenv to the server?

If I install a virtualenv on my local machine, activate it and try to run python3 then it works fine (with the imported modules). However, after I send it to the live server (using scp and filezilla) it gives the error:
-bash: /<path>/venv4/bin/python3: cannot execute binary file: Exec format error
This also happens with python and python3.8 in the same package.
I have tried reinstalling virtualenv and pipx, recreating the virtualenv and reuploading a few times.
It seems that it can't find the module, as when I activate the virtualenv on the live server and type "which python3" then it shows me the system python3:
/usr/bin/python3
It also does not work if I try to execute the venv's python3 directly, using the full path.
The reason I'm doing this is because the old virtualenv I was using has stopped working because it can't seem to find the installed modules anymore. I'm not sure why.
Any help would be much appreciated.
I believe some pip packages contain more than just python code, and must be compiled. If your host OS is different from your server OS, or you have different libraries installed, the host-compiled code will not be compatible with your server.
Common practice is to create a file with a list of required packages, using something like
pip freeze > requirements.txt
and rebuild the environment on the server, using something like
pip install -r requirements.txt

Two different /site-packages on macos

I want to use flask, I have installed it twice. I have two different site-packages directories for python.
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/
/usr/local/lib/python3.7/site-packages
After installing pip I wanted to use it so I made an alias to the path
alias pip='/usr/local/bin/pip3.7'
having this I tried to install flask
pip install flask
This did install flask to:
/usr/local/lib/python3.7/site-packages/flask
But when I tried to use it within the python prompt
$ python
>>>import flask
I got an error for 'flask', the code wouldn't run. So I figured I would try installing it with sudo
sudo pip install flask
This then introduced me this path:
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/
So this time when I tried to import flask, it worked!
$ python
>>> import flask
>>>
So, my question is which /site-packages directory should I alias pip to? I'm guessing should change to the Library/Frameworks one since that got my flask import working but I'm not sure.
Any information or recommendations on these two paths would be great, I'm new to the mac+python workflow.
Try this:
import sys
print(sys.path)
to see what you have in your Python. I suspect your pip and python does not match, e.g., one from homebrew and another come with macOS (which might make your module incompatible if they are compiled binaries). The above command prints where your python console will look for a module. If that doesn't fit where your pip will install to, try set up PYTHONPATH before running python, e.g., in bash
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.7/site-packages/
python

Add external libraries (dependencies) and reference them correctly in my python code

I try the following code to see if the library sodium can be located
import ctypes
import ctypes.util
# Taken from line 33 https://github.com/bgaifullin/pysodium/blob/master/pysodium/__init__.py
o = ctypes.util.find_library('sodium')
print o
This always returns "none"
Please how do I add external libraries (dependencies) and reference them correctly in my python code.
EDIT:
I am trying to work with pysodium it has a dependency on libsodium
I have downloaded libsodium, but i'm new to python...
I'm actually using PTVS 2.1 to get up to speed running python in my familiar dev environment.
If I understood you correctly. What you want is to import a library.
Put the pysodium directory under the script you want to use and then simply do
import pysodium
It is as simple as that.
Usually, what you do is install the libraries on your system, or in a virtualenv, and import them to your python script. Cloning the repository will not generally help unless the libraries you want to import are in the same directory as the script you're importing from.
I, personally, would recommend using virtualenv and pip together hand in hand. Read up on virtualenv, it will come very handy.
Assuming you have both virtualenv and pip, all you need to do is the following
virtualenv venv
source venv/bin/activate
pip install pysodium
This should create a virtualenv container, activate it and install pysodium inside. Your script will only work when the virtualenv is activated. You can deactivate it using the command deactivate.

Python create standalone .pyc file

is there any method to create standalone .pyc program that will contain all modules inside, I just don't want to install all modules on every computer where I want to run this program. If this is not possible what else can I do?
You could install python packages locally inside your project, using command:
pip install -t <destination_folder> <package_name>
For example:
pip install -t . mock
Will install mock library into current directory. Then, when you do import mock in the files from that folder, you will be given local file.
You could also install all packages into subfolder of your project called lib or similarly, and than before you import that package call:
import sys; sys.path.insert(0, path_to_lib_folder)
You need to create virtual python environment.
There are two ways:
VirtualENV. It creates virtual environment. So you can install python modules in it and just copy to another server.
(RECOMMENDED) Buildout. It also creates virtual environment. However you don't need to install all things and update every time you need. You just need to write simple buildout config and buildout install everything for you and keeps it up to date. Also buildout can install software which may be non-Python-based, for example some kind of database and so on. And everything will be installed locally in virtual environment.

No module named flask using virtualenv

I am following these steps to learn flask http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world/page/0#comments
I ran this command to create the virtual env:
python virtualenv.py flask
When I try to start flask using the python.exe file in my project scripts directory, it says
No module named flask
My PATH is set to the python directory that virtualenv installed. Why is it not able to find flask?
I initially started with the official Flask quickstart guide and was able to get the webserver to run, but with this virtual env install it is not working.
Make sure your virtualenv is activated. Then You check on the PYTHONPATH of that virtualenv. Is there a flask package (folder) installed in that directory.
If you unsure whether you have installed flask, just run the following command to see all the packages you have installed pip list or pip show flask. Do you see flask there? If not you can run pip install flask
This error can also appear if you start your Flask python server using ./run.py or similarly use file associations to start your server. Then the python command in the association will be used instead of your virtual environment python command. Use python run.py instead. See how my run.py innocently assumes /usr/bin/python?
#!/usr/bin/python
# run.py
from app import app
app.run(debug=True,host='0.0.0.0',port=5000)
I had this same problem on three Raspberry Pi units at the same time; beat my head against the wall trying to fix it for several hours (reinstall flask via pip, apt and aptitude - no joy).
Instead of:
pip install flask
I finally tried:
pip install Flask
Worked like a charm.
Make sure you run your script after you've activated your virtualenv. On OS X you'd see (virtual_env_name) at the start of each terminal line. To do this:
cd to your virtualenv's directory and type . bin/activate
cd to the directory containing the .py file you want to run at app launch in the browser
Now enter python file_name.py, for me the file name was routes.py following this example
This problem can also arise if the port is not available.
Try running on different port.
Activate your virtual environment first with
source bin/activate envName
Then try to run your command again
If nothing else helps, check that in your code it is:
from flask import Flask
I've tried many things before I've noticed my mistake. I had this in my code:
from Flask import Flask
When I have changed capitalized letter for the module name, i.e. flask then everything worked.
On Windows even if you see (virtual_env_name) in the cmd line it is possible that the virtual environment is not completely activated. Deactivate/reactivate and try again.
I am running Python on windows 7. I had same problem No module named flask.
I tried reinstalling python, venv but it did not work.
Finally i run it like this
Install venv the usual way
go to scripts directory and activate
C:\Python34\microb>c:\Python34\microb\fla\scripts\python run.py
here microb is my project and fla is venv
In Python 3.x
pip3 install flask
Worked fine for me.
Thanks & Regards
For those of you on Windows who are running into this problem, have activated your venv and flask is installed in the right directory. For me I realized it was looking for flask but the file was called flask.exe. I renamed it and it worked perfectly.
Try below lines :
$ python3.7 -m venv env
$ source env/bin/activate
(env)$ pip install yourpackages
(env)$ python app.py

Categories

Resources