简体   繁体   中英

Cannot getting the Current directory Path in Python (Linux)

I was trying to execute the Program that I coded in Windows, in a Linux environment, I was consistently getting error on the line that was supposed to Import the file from the sub-folder.

The program gives the following error,

Traceback (most recent call last):
  File "BlackBox.py", line 26, in <module>
    from BB_Files import BB_Expand
ImportError: No module named BB_Files

Despite the presence of the file BB_Expand inside BB_Files folder, I am still getting the error.

I have also tried appending the path of my current directories in Python,

sys.path.append("/home/pe/Desktop/AES")
# Sub-Folders of AES are also accessible
sys.path.append("/home/pe/Desktop/AES/BB_Files")

But Still no Luck,

This is the File Structure,

/home/pe/Desktop/AES/Main.py
/home/pe/Desktop/AES/BB_Files
/home/pe/Desktop/AES/BB_Files/BB_Days.py
/home/pe/Desktop/AES/BB_Files/BB_Expand.py
/home/pe/Desktop/AES/BB_Files/BB_Steps.py 

this is the output of ls -l command,

drwxrwx--x 4 pe users  4096 Oct 26 21:43 BB_Files
-rw-rw---- 1 pe users 15284 Oct 26 22:04 Main.py

This is some initial code in the file,

import sys    # sys.argv ; sys.path, sys.exit
import os
import hashlib
import struct # Interpret strings as packed binary data
import getopt # for Runtime arguments
import time
from datetime import date

# Append Paths from where the Files would be Imported.
sys.path.append("/home/pe/Desktop/AES")
# Sub-Folders of AES are also accessible
sys.path.append("/home/pe/Desktop/AES/BB_Files")
# Sub-Fodlers of BB_Files are also accessible now (Tables)
from BB_Files import BB_Expand
from BB_Files import BB_Steps
from BB_Files import BB_Days

This is the line giving an error,

from BB_Files import BB_Expand

The program doesn't run after this line because the Python couldn't find this Module.

But when I tried to print the path of the current directory I get nothing, have a look,

print("Path is:",os.path.dirname(__file__))
print("sufiyan")

Output:

('Path is:', '')
sufiyan
Traceback (most recent call last):
  File "BlackBox.py", line 25, in <module>
    from bbfiles import bbexpand
ImportError: No module named bbfiles

I want to know why the path is not being printed while its printing fine in Windows. All i get is a black space instead of the path of the current directory.

Obviously the following line will throw an ImportError error

from BB_Files import BB_Expand 
## if you comment this the next immediate line will give you same error

Since this is your first attempt to import your module from a package

so, when you say

from <something> import <something-else>

means that, you are importing a module/class/function from a package/module

in your case it's a package , probably a file called __init__.py to be placed in your directory, so the python will consider the directory as a package.

## try this to get your directory name
print __file__
print "Path is:", os.path.dirname(os.path.abspath(__file__))

Try adding an __init__.py file to both directories. It doesn't have to contain anything but it must exist. When Python tries to load a module directory it first tries to load this file since it can contain additional instructions as to the loading of the module (such as the ability to import platform-dependent code from the correct file). If Python doesn't find the file it might not consider the directory as a Python module and fail to import files from it.

Read more about it in the documentation .

Adding an empty file named __init__.py inside the /home/pe/Desktop/AES/BB_Files directory should solve the problem. Read more on Python Docs .

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