Python Forum
project/main.py import from project/bin/APP_NAME
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
project/main.py import from project/bin/APP_NAME
#1
I'm trying to import modules (executables for some apps) in Python 3.6 from a folder inside my projec. I need main.py to import 'app.py' from ./bin/APP_NAME. ./bin/APP_NAME is for the app itself and ./data/APP_NAME is a place for the app to store data in.  Heres the scructure:

~/.project
            /bin
                   /APP_NAME
                                    /app.py
                   /APP_NAME2
                                    /app2.py
            /data
                   /APP_NAME
            /main.py  (the emain program)
            /git.py

How can I assign each apps bin and data folders to a variable in main.py so each app is isolated from the rest of the project, similar to how apps work on Android, and be able to import them in this way from main.py?

EDIT: I'm also dabbling a little in C++ and Bash so if it can't be done in Python alone, I can mix these languages if necessary!
Reply
#2
you should start by including an __init.py__ file
containing the tree structure above. this should be placed in the top
directory for the project.
then create a package, see: https://python-forum.io/Thread-Basic-Pac...-py-Freeze
if you install the package with pip, a working copy will be placed in the site-packages directory
of your python package.

Now it can be imported, same as a system package.
Reply
#3
See, that's the problem. I'm not using pip, I wrote a module (git.py) that integrates with Github and installs "apps" (each in its own Github repository) using my own install system (incomplete) and using a sqlite3 database to index these packages into a repository (aslo incomplete). git.py creates the directories "project/data/git/source/(Developers Github username)" and "project/data/git/source/(Developers Github username)/(Github repository)", then changes into "project/data/git/source/(Developers Github username)/(Github repository)" to clone the Github repository ( git.clone() ) then runs my own install scripts ( git.install() ).

For example, (we'll assume were trying to run a pip setup.py file here) if setup.py was in project/data/git/source/(username)/(repo name)/, how could main.py (from the interpreter) run this setup.py file?
Reply
#4
(Jul-19-2017, 12:36 PM)Myersj281 Wrote: For example, (we'll assume were trying to run a pip setup.py file here)
You can not run pip setup.py it's python setup.py that point to a module(singel file) or package(folder and files).
Your stuff look a little messy  Confused
This stuff can be tricky to understand.

Here a example of a package.
|-- project\
__init__.py 
   |-- bin\
    __init.py__
       |-- app_1\
        __init__.py
        app_foo.py
      |-- app_2\
       __init__.py
       app_bar.py
project/__init__.py
from .bin.app_1 import app_bar
from .bin.app_2 import app_foo
project/bin/app_1/app_foo.py
def foo():
   return 'i am foo'
prosject/bin/app_2/app_bar.py
def bar():
   return 'In am bar'
So now is project a package,__init__.py is the glue.
I make change in top __init__.py,to make a cleaner import.
Test of package project:
λ ptpython
>>> from project import app_foo, app_bar

>>> app_foo.foo()
'i am foo'
>>> app_bar.bar()
'In am bar'

# Or
>>> import project

>>> project.app_foo.foo()
'i am foo'
>>> project.app_bar.bar()
'In am bar'


From web-app and mobile stuff,making a module/package may not be the best approach or make any sense.
Look at my tutorial here weather-app
So here use git clone and explain how to run it.

To download code and also run it:
git clone https://[email protected]/snippsat/weather-app.git
pip install flask requests
In terminal cd into weather app folder then:
python app.py
This start the web server,in browser http://127.0.0.1:5000/

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Organization of project directories wotoko 3 364 Mar-02-2024, 03:34 PM
Last Post: Larz60+
  Rock Paper Scissors Project in Python ankitdixit 8 4,730 Feb-23-2024, 03:14 PM
Last Post: DPaul
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 945 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  Need help with coding project jjvoc 4 424 Feb-14-2024, 01:14 PM
Last Post: jjvoc
  Help needed in finding a library for this project PythonEnthusiast1729 7 720 Dec-27-2023, 11:27 AM
Last Post: PythonEnthusiast1729
  dynamic variable name declaration in OOP style project problem jacksfrustration 3 719 Oct-22-2023, 10:05 PM
Last Post: deanhystad
Music Help with: Audiobook Library/Copier Project eleven43 0 455 Sep-14-2023, 04:17 PM
Last Post: eleven43
  programming OpenOffice with External Python Project bobthebuilder44 1 733 Apr-07-2023, 06:02 AM
Last Post: buran
  My first project as a beginner lil_e 4 1,083 Feb-27-2023, 08:19 AM
Last Post: lil_e
  Web project and running a .py file emont 0 620 Dec-11-2022, 11:15 PM
Last Post: emont

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020