Is It Ok to Use Dashes in Python Files When Trying to Import Them

Is it ok to use dashes in Python files when trying to import them?

You should check out PEP 8, the Style Guide for Python Code:

Package and Module Names Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

Since module names are mapped to file names, and some file systems are case insensitive and truncate long names, it is important that module names be chosen to be fairly short -- this won't be a problem on Unix, but it may be a problem when the code is transported to older Mac or Windows versions, or DOS.

In other words: rename your file :)

How to import module when module name has a '-' dash or hyphen in it?

In Python 2, you can't. foo-bar is not an identifier. rename the file to foo_bar.py


It's possible since Python 3.1+, see Julien's answer.


If import is not your goal (as in: you don't care what happens with sys.modules, you don't need it to import itself), just getting all of the file's globals into your own scope, you can use execfile

# contents of foo-bar.py
baz = 'quux'
>>> execfile('foo-bar.py')
>>> baz
'quux'
>>>

Python module with a dash, or hyphen (-) in its name

You can do that using __import__(). For example:

foobar = __import__("foo-bar")

But you really should rename the module instead. That way you can avoid confusion where the filename of the module is different from the identifier used in the program.

I can't import a library because of a dash in Python

You can't. Take a look at this solution. You would need to rename the module with an underscore.

In your case, the python-binance library doesn't need to be imported like that. All you need to do is import the client like this:

from binance.client import Client
client = Client(api_key, api_secret)

Using hyphen/dash in python repository name and package name

To answer your 1st point let me rephrase my answer to a different question.

The biggest source of misunderstanding is that the word "package" is heavily overloaded. There are 4 different names in the game — the name of the repository, the name of the directory being used for development (the one that contains setup.py), the name of the directory containing __init__.py and other importable modules, the name of distribution at PyPI. Quite often these 4 are the same or similar but that's not required.

The names of the repository and development directory can be any, their names don't play any role. Of course it's convenient to name them properly but that's only convenience.

The name of the directory with Python files name the package to be imported. Once the package is named for import the name usually stuck and cannot be changed.

The name of the distribution gives one a page at PyPI and the name of distribution files (source distribution, eggs, wheels). It's the name one puts in setup(name='distribution') call.

Let me show detailed real example. I've been maintaining a templating library called CheetahTemplate. I develop it in the development directory called cheetah3/. The distribution at PyPI is called Cheetah3; this is the name I put into setup(name='Cheetah3'). The top-level module is Cheetah hence one does import Cheetah.Template or from Cheetah import Template; that means that I have a directory cheetah3/Cheetah/.

The answer to 2 is: you can have dashes in repository names and PyPI distribution names but not in package (directories with __init__.py files) names and module (.py files) names because you cannot write in Python import xy-zzy, that would be subtraction and SyntaxError.

Point 3: The site and the repository names are scikit-learn, as well as the distribution name, but the importable package (the top-level directory with __init__.py) is sklearn.

PEP 8 has nothing to do with the question as it doesn't talk about distribution, only about importable packages and modules.

How import package from PyPI with hyphen in name?

Starting in python3.x you can use importlib for some generic module that actually installs with a hyphen in the name. I will use neat-python as an example even though I have been informed that it actually installs as neat:

--myscript.py--

import importlib
neat = importlib.import_module("neat-python")
# to then call "mymodule" in neat
neat.mymodule(someobject)

python module resides in a repository whose name contains dash characters

Use importlib.import
module
:

import importlib
fabfile = importlib.import_module('my-repo-name.fab.fabfile', None)
X = fabfile.X

But you should really just change the name of the repository. To transition, you can create a temporary symlink with

$ mv my-repo-name my_repo_name
$ ln -s my_repo_name my-repo-name


Related Topics



Leave a reply



Submit