I read these guides to create my project.
Make the Project Installable
Deploying with Setuptools
First, creating a tar zip file.I create many files in my project folder.
setup.py
from setuptools import setup, find_packages
setup(
name='WebMonitor',
version='1.0',
long_description=__doc__,
packages=find_packages(),
include_package_data=True,
zip_safe=False,
install_requires=['Flask>=0.12']
)
MANIFEST.in
include schema.sql
include __init__.py
include auth.py
include blog.py
include db.py
graft static
graft templates
global-exclude *.pyc
setup.cfg
[egg_info]
tag_build = .dev
tag_date = 1
[aliases]
release = egg_info -Db ''
Then I run this command python setup.py release sdist to build a release package.
Second, install and run this app.
Create a virtual environment with the command virtualenv env
Activate the env with env\Scripts\activate
Install the release package by pip install WebMonitor-1.0.tar.gz
set FLASK_APP=WebMonitor
Then run my app flask run -h 127.0.0.1 -p 5001. I get an error output:
Serving Flask app "WebMonitor"
Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
Debug mode: off
Usage: flask run [OPTIONS]
Error: Could not import "WebMonitor".
Does anyone know how to solve this problem? Thank you very much.
As it is production env
in cmd,
set FLASK_APP=WebMonitor.py
set FLASK_ENV=development
First ensure that you are at the root of the application file. in case it persist, Deactivate the virtual environment then activate it.
Related
Environment
PC1: Dev-machine, online, Python install dir: C:\Python310
PC2: Target machine, offline, Python install dir: C:\Program Files\Python310
Doing
Write source and run command in workdir pip install -t ./out ./ on PC1.
Copy files under out dir from PC1 to PC2.
Open term and invoke exe file on PC2.
Then I got message Fatal error in launcher: Unable to create process using '"C:\Python310\python.exe" "C:\Program Files\Python310\Scripts\my_app.exe" ': ??????????????????.
How can I build for PC2?
Folder structure
┗━ my_app
┣━ setup.py
┗━ my_app
┣━ __init__.py
┣━ __main__.py
┗━ main.py
File contents:
setup.py
from setuptools import setup, find_packages
setup(
name = 'my_app',
packages = find_packages(),
entry_points = {
'console_scripts': [
'my_app = my_app.main:main',
],
},
)
my_app/__main__.py
from .main import main
main()
my_app/main.py
def main():
print('hello world')
Constraints
without cx_freeze, pyinstaller, py2exe or similer third party packages
actual my_app requires external packages(ex: tqdm)
Run command python setup.py bdist and copy dist/my_app...zip content to target machine, its resolved my question.
https://docs.python.org/3/distutils/builtdist.html
I am trying to serve static files from flask app. It is working fine in development environment from visual studio code.
I made .whl file from the project including static folder.
In setup.py
from setuptools import find_packages, setup
setup(
name='techportal',
#packages=['techportal'],
packages=find_packages(),
include_package_data=True,
install_requires=[
'flask',
],
)
In MANIFEST.IN
include myportal/static/*
Then I installed wheel and created wheel file
pip install wheel
python setup.py bdist_wheel
Installed it using pip
pip install ./myportal-0.1.whl
In linux production server I run app using:
waitress-serve --port=8080 'myportal:app'
But I am not able to open url using:
http://192.168.0.1:8080/static/index.html
I think you should use graft for folders and include for files, i.e,
graft myportal/static
global-exclude *.pyc
"global-exclude *.pyc" just to exclude .pyc files, you can ignore that line in your MANIFEST.in file.
I create a python package. The configuration should be placed in /etc/myapp if installed globally or in ~/.myapp if installed for user only (pip install myapp --user).
I can do the first point with this in the setup.py file:
data_files = [("/etc/myapp", ['application.properties'])
But how can I place this file in ~/.myapp if installed for user only?
I have a python project with the following structure:
/project
/bin
executable
/app
__init__.py
a.py
b.py
c.py
From within the /project directory I try to run the executable, which depends on modules in the app package:
./bin/executable
However, when I try this, python fails to find any modules defined in the app package, giving the ImportError: No module named XYZ error.
From what I understood, the presence of __init__.py in the app directory should mark it as a module?
What am I doing wrong?
If you add the following to your executable (before you try importing your app module):
import sys; print sys.path
...
import app
You'll see that the current working directory is not included in the path. I suggest you create a proper Python package by adding a setup.py file. You can then install your package and your executable should work just fine.
Here is a simple setup.py file:
from setuptools import find_packages, setup
config = {
'name': 'app',
'version': '0.1.0',
'description': 'some app',
'packages': find_packages(),
}
setup(**config)
I prefer to install into a virtualenv:
virtualenv venv
source venv/bin/activate
python setup.py install
./bin/executable
Now, if you deactivate that virtualenv and try running your executable again, you will get your original ImportError.
Take a few minutes and read through the Python Packaging User Guide.
Also, you might want to use python setup.py develop instead of python setup.py install.
I'm having an issue with running setup.py/pip in a chroot environment.
Here's the scoop:
Normal directory location:
/local/my_dir/project/src/qa/libs
Chroot-ed location
/src/qa/libs
Here's my setup.py file:
#!/usr/bin/env
from __future__ import (unicode_literals, print_function, division,
absolute_import)
from setuptools import find_packages, setup
test = [
'mock',
'pytest',
'pytest-cov',
]
setup(
name='libs',
version=0.1,
description='Some desc',
long_description=open('README').read(),
author='insert_author_here',
author_email='insert_email_here',
packages=find_packages(),
package_dir={},
include_package_data=True,
tests_require=test,
install_requires=[],
keywords=['qa', 'framework'],
extras_require={
'test': test,
}
)
When running python setup.py develop in the libs directory everything goes swimmingly during the install until the very end.
Installed /src/qa/libs
Processing dependencies for libs==0.1
Finished processing dependencies for libs==0.1 # <-- It hangs here
This doesn't happen when I'm not currently in chroot (required for the environment) and it seems like setuptools/distribute is getting stuck in a recursive filesystem looking for things to clean up. Any idea how to fix this?
Installing a requirements.txt file with pip doesn't have any problems like this, so I think it might be the structure of the setup.py file.
It turns out the hang occurred during during the bash script that created the virtualenv and installed this package. I figured this out by executing the script with the bash -x my_script command, which showed the actual executing command when the hang occurred.
The setup.py file correctly installs the package and exits successfully.