如何从父目录的父目录的子目录导入文件?我知道这没有意义,所以让我解释一下。我想将文件test.py导入文件example.py。
这是目录结构:
directory1
+-- directory2
+-- test.py
+-- directory3
+-- directory4
+-- example.py
答案 0 :(得分:0)
只需确保文件夹中还包含__init__.py
,就可以将其作为软件包包含在内
答案 1 :(得分:0)
您有多种选择:
os.chdir
并将目录更改为directory1的根目录,并调用directory2 / test.py from [directory] import [module]
答案 2 :(得分:0)
在每个目录,父目录和子目录内添加 program putiff
c This program is solely intended to read the data from the .tif files made by the CCD camera
c PIXIS 1024F at beamline 1-BM at the Advanced Photon Source, so that they can be manipulated
c in fortran. It is not a general .tif reader.
c A little bit extra work may make this a reader for baseline .tif files,: some of the
c information below may help with such an implementation.
c
c The PIXIS .tif file is written in hex with the little-endian convention.
c The hex numbers have two 8-bit bytes. They are read with an integer(kind=2) declaration.
c When describing an unsigned integer these cover numbers from 0 to 65535 (or 2**16-1).
c For the PIXIS files the first two bytes are the decimal number 18761. The TIFF6 specification
c gives them as a hexadecimal number (0x4949 for a little-endian convention, 4D4D for the
c big-endian convention. The PIXIS files are little-endian.
c
c The next two bytes should be 42 decimal, and 0x2A.
c
c The next 4 bytes give the byte offset for the first image file directory (IFD) that contains
c all the other information needed to understand how the .tif files are put together.
c This number should be read together as a 4 byte integer (kind=4). These (unsigned) integers
c go from 0 to 2**32-1, or 4294967295: this is the maximum file length for a .tif file.
c For the PIXIS this number is 2097160, or 0x200008: in between are the image date for the
c PIXIS's 1024x1024 pixels, each with a two-byte gray range from 0 to 2**16-1 (or 65535 decimal).
c Therefore the PIXIS image can be read without understanding the IFD.
c
c The line right below the hex representation gives the byte order, for the
c little-endian convention indicated by two first bytes. It's 4949 for little-endian,
c in both the first and in the second byte separately. The byte order is then least importan
c part first; with two bytes together, it is byte by byte. For big-endian it is 4D4D.
c
c One way to confirm all this information is to look at the files
c with a binary editor (linux has xxd) or a binary editor (linux has hexedit).
c For the PIXIS image .tif file, the first 8 bytes in hexedit are indeed:
c 49 49 2A 00 08 00 20 00
c For a little-endian file, the bytes are read from the least important to the
c most important within the two-byte number, like this:
c 49 49 2A 00 08 00 20 00
c (34 12) (34 12) (78 56 34 12)
c Here the byte order is indicated below the numbers. The second two-byte number is
c therefore 2+2*16+0*256+0*4096, or 42. Likewise, the last 4-byte number is 0x00200008.
c
c (When the individual byte are read in binary (with 'xxd -b -l 100') this gives
c for the hexadecimals 49 49 2A 00 08 00 20 00
c binary 01001001 01001001 00101010 00000000 00001000 00000000 00100000 00000000
c in ASCII I I * . . . . . )
c After the PIXIS data comes the so-called IFD (Image File Directory).
c These contain 209 bytes. They mean something, but what I do not know. I printed them
c out one by one at the end of the program. Perhaps they are better read in two-byte units
c (right now they are read as 'integer(kind=1); integer(kind=2) may be better). But, then
c there's an odd number so you have to read one separately.
c I want to know these only because I want to use the same .tif format to
c write the results of rctopo (the max, the COM, the FWHM, and the spread).
c I know what's in the first 8 bytes, and what the data are, so I can just
c copy the ifd at the end and count on getting a good .tif file back.
c It's sort of stupid, but it should work.
use iso_fortran_env
implicit logical (A-Z)
integer :: j,jmin,jmax
integer :: k,kmin,kmax
integer :: ifdlength
data jmin,kmin/1,1,/
parameter(jmax=1024,kmax=1024)
parameter(ifdlength=209)
c 8-byte header that starts the PIXIS data file
integer (kind=2) :: tifh12,tifh34 ! each two (8-bit) bytes
integer (kind=4) :: tifh5678 ! 4 bytes
c open and read the file now that you have the correct file name in the sequence
open(newunit=unt,file='tiff_file,access='stream',iostat=ios)
if (ios /= 0) then ; call problem(ios,'read_in_samples'); end if
read (unt) tifh12,tifh34,tifh5678,greyread,ifd
close (unt)
stop
end
文件
__init__.py
parent/
__init__.py
package_1/
__init__.py
package_2/
__init__.py
package_3/
__init__.py
如果不起作用,请尝试配置from parent.package_1 import ....
,以防 IDE (如 PyCharm ),在运行/调试配置中会有一个标记(复选框)
或者您可以使用PYTHONPATH
sys.path.insert(...)
您可以使用import sys
sys.path.insert(0, module_full_path)
答案 3 :(得分:0)
您在这里= ^ .. ^ =
from pathlib import Path
import sys
path = Path(__file__).parents
sys.path.append(str(path[2] / 'directory2'))
import test