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
- Wrap the file name in quotation marks like this
"your file.txt"
- 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
Python: Draw Line Between Two Coordinates in a Matrix
How to Store Python Dictionary in to MySQL Db Through Python
Python Creating Dictionary from Excel Data
How to Add a Path to Pythonpath in Virtualenv
Update Tkinter Canvas in Mainloop()
How to Execute Two Commands in Terminal Using Python'S Subprocess Module
Python Db-Api: Fetchone VS Fetchmany VS Fetchall
How to Remove the Double Quote When the Value Is Empty in Spark
How to Remove Zeros After Decimal from String Remove All Zero After Dot
Tensorflow - Valueerror: Failed to Convert a Numpy Array to a Tensor (Unsupported Object Type Float)
How to Check If a String Column in Pyspark Dataframe Is All Numeric
Python Xlsxwriter Set Border Around Multiple Cells
How to Iterate Through a List of Dictionaries in Jinja Template
Opening a Word Document That Has a Password Using Docx Library
How to Extract Column Value Within Square Brackets in Pyspark
How to Make Python Get the Username in Windows and Then Implement It in a Script
How to Fill a Column With the Value of Another Column Based on a Condition on Some Other Columns