How to Import a File in Python With Spaces in the Name

How do you import a file in python with spaces in the name?

You should take the spaces out of the filename. Because the filename is used as the identifier for imported modules (i.e. foo.py will be imported as foo) and Python identifiers can't have spaces, this isn't supported by the import statement.

If you really need to do this for some reason, you can use the __import__ function:

foo_bar = __import__("foo bar")

This will import foo bar.py as foo_bar. This behaves a little bit different than the import statement and you should avoid it.

Import python module directory with "space" char

Also you can try it this way:

# object_detection_2d_data_generator.py
import sys
sys.path.insert(0, '/content/gdrive/My Drive/mrcnn_fire/ssd/python_files')

from object_detection_2d_data_generator import DataGenerator

How do you import a file in python with spaces in the name?

You should take the spaces out of the filename. Because the filename is used as the identifier for imported modules (i.e. foo.py will be imported as foo) and Python identifiers can't have spaces, this isn't supported by the import statement.

If you really need to do this for some reason, you can use the __import__ function:

foo_bar = __import__("foo bar")

This will import foo bar.py as foo_bar. This behaves a little bit different than the import statement and you should avoid it.

Python 3.5: Importing a specific function from a file with a space in its name

Editing the answer as requsted:

Source: How do you import a file in python with spaces in the name?.

Importing single function from a module with spaces in name without using importlib is simply impossible, since your only weapon here is __import__.

Your only option is to import the whole module and only keep functions you like. But it still imports the whole module.

Important notice

Getting rid of spaces and other non-alphanumeric symbols from module names is strongly recommended.

Example

File 1.py

def foo():
print("Foonction.")

def spam():
print("Get out!")

main.py

globals()["foo"] = getattr(__import__("File 1"),"foo")

how to import .txt with space in filename

  1. Wrap the file name in quotation marks like this "your file.txt"
  2. If you have a filepath, use the raw string (and backslashes in the copied filepath) by placing a r before the quotation marks:
    r"C:\Folder\Subfolder\another_one\your_file.txt"

Unit Testing File with Space in the Name

Seems like option 2 probably works best.
Option 1 and 2 are your best bets (yes 3 is a bit overkill), and although 2 seems excessive, it does isolate your python logic from your py2app requirements - now Application Name.py is a "py2app wrapper file", and application_name.py contains your actual logic.

This works better than Option 1 because separation of responsibilities is generally preferred. If you come up with other requirements for your application name, you'd want to have to deal with just the "py2app wrapper file", and not change anything related to actual logic.

Your current workflow works too, but it does mean more manual renaming when you want to run unit tests - what if you want to automate the unit testing process?



Related Topics



Leave a reply



Submit