简体   繁体   中英

Module/Project structure in Python

I'm trying to build a simple project in Python,however I can't seem to wrap my head around how it's done despite reading the documentations over and over and reading example code.

I'm semi-new to Python, having a background of only single-file scripting. However I made numerous Node.js and Java projects with multiple folders and packages. In Python However I can't seem to grasp the project structure and workflow mechanisms- init .py, virtualenvs, setuptools etc. it all seems very alien and unintuitive to me, hurting my overall productivity(ironically Python is supposed to be a language aimed at productivity, right?)

My project has this example structure:

package_test  (package)
    |
    |------- __init__.py
    |------- main.py (entry point)
    |
    |------- /lib (modules)
    |           |
    |           |----- __init__.py
    |           |----- lib.py
    |
    |------- /Tests
    |           |
    |           |----- __init__.py
    |           |----- test.py

in main.py:

 from lib import lib

 lib.libtest()

in lib.py:

 def libtest():
     print("libtest")

in test.py:

 from lib import lib

 lib.libtest()

Running main.py works, However when running test.py it cannot find the module. I have tried solutions such as appending to sys.path, putting '..' before lib, and more- none have worked.

This is just an example, but I wish to develop more complex projects in Python with multiple subfolders in the future(I think Python has some cool features and nice libraries), yet this problem keeps bothering me. I never had to think about these issues when developing in Java or in Node, not to mention stuff such as virtualenv etc.

Thank you in Advance

Your test.py needs to know lib.py is one level above it and inside the lib folder. So all you should need to do is append the path of your lib folder before running the import of lib.py into test.py. Something like

import sys
sys.path.append("..//lib")
import lib

Please note that the answer above assumes you run on Windows. A more comprehensive approach would be to replace the windows paths with proper os.path syntax. Also another assumption made by this code is that you will run your test.py from it's folder, not with an absolute/relative path.

import sys
import os.path
sys.path.append(os.path.join(os.path.pardir, 'lib'))
import lib

If you want to run your test.py with a path, your code could look like this:

import sys
import os.path
# os.path.abspath(__file__) gets the absolute path of the current python scrip
# os.path.dirname gets the absolute path of whatever file you ask it to (minus the filename)
# The line below appends the full path of your lib folder to the current script's path
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), os.path.pardir, 'lib'))
# This should allow test.py to import libtest regardless of where your run it from. Assuming your provided project structure.
from lib import libtest
libtest()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM