Article 4Z4MM [python] To be used by "import", what characters and symbols can local filenames have?

[python] To be used by "import", what characters and symbols can local filenames have?

by
dedec0
from LinuxQuestions.org on (#4Z4MM)
I started learning python with a book. Python 3, to be exact.

The book is nice. It goes talking about things, and adding them to the code built until that point.

Right now, i have a file with a few 'import [library]', followed by some function definitions, followed by some code to - mostly - use those functions.

I want to play with those functions in python command line.

Calling the file from the system command line like "$ python3 filename.py" is bad. When the last line of the file finishes, python3 command exists too.

Opening the python environment and trying an import command, surprisingly, does not work.

At least, not with the filenames I have been using with this book. I created several files with names like:

section.1.3.py
section.3.2.py
exercise.4.1.13.py

But when i tried to import them in python, i got:

Code:>>> import section.1.3
File "<stdin>", line 1
import section.1.3
^
SyntaxError: invalid syntax
>>> import "section.1.3"
File "<stdin>", line 1
import "section.1.3"
^
SyntaxError: invalid syntax
>>> import <section.1.3>
File "<stdin>", line 1
import <section.1.3>
^
SyntaxError: invalid syntax
>>>I thought: "The problem may be the dots in the filename." So i changed them to be ",", but the error is basically the same:

Code:

>>> import exercise,4,1,13
File "<stdin>", line 1
import exercise,4,1,13
^
SyntaxError: invalid syntax
>>> import "exercise,4,1,13"
File "<stdin>", line 1
import "exercise,4,1,13"
^
SyntaxError: invalid syntax
>>> import <exercise,4,1,13>
File "<stdin>", line 1
import <exercise,4,1,13>
^
SyntaxError: invalid syntax
>>>I tried to look in docs.python.org/3/reference/import.html for help, but it does not mention.

I read a few stackoverflow questions, but they do not say what i need.

Which characters and symbols are allowed in local python filenames that will be imported? Is there another way to import code from local files? Or doing what python calls packages is the only way to go? (and is something i am unable to do now, i think)

About the errors with import command, it works with a local file named s.py. We do "import s", and it will work. But this leads me to the question: in which places, and in which order "import" will look for names we ask it to load? What happens if "s.py" file defines a function "print"?

---------------------------------------

Surprisingly, unicode filenames work:

Code:$ ls seiio.py # much code inside this file
seiio.py
$ python3
[...]
>>> import seiio
>>>latest?d=yIl2AUoC8zA latest?i=piLsIJjPTCU:jLj0QlIhBvc:F7zBnMy latest?i=piLsIJjPTCU:jLj0QlIhBvc:V_sGLiP latest?d=qj6IDK7rITs latest?i=piLsIJjPTCU:jLj0QlIhBvc:gIN9vFwpiLsIJjPTCU
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments