I'm currently trying to bundle my Python (3.4.4) application with PyInstaller. I'm following the PyInstaller documentation about how to build a .spec file.
This is how my project file tree looks like:
project/
__init__.py
main.py
ui.py
Lib/
__init__.py
History.py
Command.py
Graphics.py
Tools.py
According to the documentation, all I have to do is to run the pyi-makespec main.py function and the Lib module will be detected as long as it is imported from the main.py file.
Documentation:
Because your script includes the statement import helpmod, PyInstaller will create this folder arrangement in your bundled app
https://pythonhosted.org/PyInstaller/spec-files.html#using-data-files-from-a-module
This is how the beginning of the my main.py looks like
# main.py
from Lib.Command import Command
from Lib.History import History
from Lib.Graphics import Graphics
import Lib.Tools as Tools
When I try to run the app afterwards, all I get is this error. I probably missed something, does someone have an idea of the problem ? :D
error log : http://pastebin.com/3dygTqfn
EDIT : Just figured out that the problem comes from my Graphics.py library, which import some bokeh tools I use to generate histograms. Still don't know why the bokeh import makes the whole thing crash, it works perfectly fine when I run it with a python interpreter...
Related
I'm creating my first package and I can not figure out how to create a simple test.py file to debug my code.
Consider the following file structure.
|--MyPackage
|--test.py
|--PackageName
|--__init__.py
|--module.py
Inside test.py I have: from .src.module import SomeClass
And of course this gives me the dreaded "attempted relative import with no known parent package" message.
I know I could just install the package and then import it with from PackageName.module import SomeClass but then I'm using the code installed on my system and not the code that I am actively editing.
There has got to be some kind of standard way for testing and debugging a package right? Despite all the searching I've done, I can't seem to find any kind of solution.
I'm running test.py with python3 test.py
If it's helpful, here is a screenshot of my actual project folder structure:
apologies if I'm going about this wrong, but I am quite new to python and can't quite figure out what the problem is! I have a simple Flask app that I'm trying to write tests using pytest for. The file structure is like this:
│── app.py
|── tests
│ ├── tests_app.py
I originally had app.py and tests_app.py in the same level and it all worked fine with tests passing, but now I have put tests in their own folder I can no longer import app without error.
I have tried the following in tests_app.py:
from app import app #this is what worked fine when app and tests were in the same folder
from ..app import app
from .. import app
and the linting error is 'Attempted relative import beyond top-level package' and when I run pytest it says 'ImportError: attempted relative import with no known parent package'.
Many thanks in advance!
p.s. I am using Python 3.8.5
You need to have app on the module search path. A quick fix would be to add the path to app.py to the PYTHONPATH environment variable:
export PYTHONPATH=/path/to/dir # where app.py is
If app.py is on the python path, then import app will work (barring any other import problems such as circular imports).
An effective and reproducible way to do this is to install your application. You can do this in develop mode, which means the code runs from its current directory. This is an understated, important part of developing a Python application, but to do it you will need to write code to package your application.
I've been developing a project in python for some time using pycharm. I have no problem running it in pycharm, but I want to try and running it from the command line (I'm using windows).
When I try to run my main file using python <filename> from within the root directory, I get a module not found error. What is pycharm doing/how can i replicate it?
Also, pycharm creates pycache folders. If my understanding is correct its compiling the files, and that makes the runtime faster. Since my runtime is already long i'd like to do the same.
I'm using python 3.6
edit
File Structure
-Root\
----scheduler\
------main.py
------matrices
------models
------rhc
------Singleton.py
------utils.py
------__init__.py
------apis\
acedb.py
metrics.py
__init__.py
------matrices\
distancematrix.py
__init__.py
------models\
branch.py
constants.py
customer.py
dispatch.py
technician.py
workorder.py
__init__.py
------rhc\
pathfinder.py
rhc.py
schedule.py
sched_time.py
tech_schedule.py
__init__.py
Edit 2
Found the solution, i had to move my main files outside of the modules
Thanks!
If you have the below folder structure for instance. add an empty file named init.py and import from your app.py, in case you have a Module1Class , you can always import it like this
from modules.module1 import Module1Class
from modules.module2 import Module2Class
Folder structure
/app.py
/modules
/module1.py
/module2.py
/__init__.py
The first time you rum app.py from terminal like python app.py, the .pyc files will be created, leaving you with the following folder structure
/app.py
/modules
/module1.py
/module1.pyc
/module2.py
/module2.pyc
/__init__.py
Please refer to the python documentation as it's very well documented on how to create modules and importing them. I'm more used to python2.7 , so my answer might not be an exact fit to newer python versions.
https://docs.python.org/3/tutorial/modules.html
From there you can learn more about __ init __.py and module creation , exporting and importing
PS: I use only text editors to develop in python, as I find pycharm a bit on the heavy side, so I cannot explain how exactly pycharm works behind the curtains.
see ModuleNotFoundError: What does it mean __main__ is not a package? for a good example of ModuleNotFoundError description (was answered fast)
I bet that pycharm is configured to use a different python interpreter of virtual environment.
I'm working on a Flask app and run into issues with trying to run a script within a module where the script is in a different directory. I've tried looking at several solutions here and on other sites and haven't been able to find something that works. I have a project structure like so:
dashboard\
app\
static\
templates\
__init__.py
jobs.py
api_fetch.py
config.py
run.py
Within jobs.py I have a function that needs to run api_fetch.py but for the life of me, I'm not sure what I need to do to that. I've tried imports with .., sys, os and nothings worked. This seems like it shouldn't be that difficult, but I'm at a loss. So far I've only needed to import modules on the same path which work fine.
in jobs.py:
from .. import api_fetch
then 'outside' dashboard:
$ python -m dashboard.app.jobs
__init__.py should be in dashboard also.
You can just import from parent package dashboard, the file structure should like this:
dashboard\
|app\
|static\
|templates\
-__init__.py
-jobs.py
-__init__.py << don't forget this!
-api_fetch.py
-config.py
-run.py
Then in jobs.py:
from dashboard import api_fench
I'm having trouble with Eclipse PyDev autocomplete when trying to use an import statement in a file in a subfolder.
My folder structure is:
src
--data
--libs
--__init__.py
--myclass.py
--logs
mainscript.py
When I use import in mainscript.py (i.e. import re) everything goes fine, however when I try to use import in myclass.py eclipse doesn't show the autocomplete display.