Can't Figure Out How To Use A Path With The IMPORT Command

This is the place for queries that don't fit in any of the other categories.

Can't Figure Out How To Use A Path With The IMPORT Command

Postby Honduras2811 » Fri Dec 20, 2013 6:25 am

(General hardware and software should be listed in the signature.)

I have been using Linux for two days and Python for about 4 hours. Before that I used Mickeysoft for more than 20 years.

I like to keep project files in a directory heirarchy away from the application I am using to create them. I'm trying to program the example on page 17 in 'Learning Python'. Not being familiar with the Linux style file system doesn't help. Basically, IMPORT can't find myfile.py, which I did create. As I understand it, myfile.py is located in the home/Rory/PythonWork directory.

Here are some of the things I have tried:

Code: Select all
>>> import myfile

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import myfile
ImportError: No module named myfile


Code: Select all
>>> import myfile.py

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    import myfile.py
ImportError: No module named myfile.py


Code: Select all
>>> import PythonWork.myfile.py

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    import PythonWork.myfile.py
ImportError: No module named PythonWork.myfile.py


Code: Select all
>>> import home/Rory/PythonWork/myfile.py
SyntaxError: invalid syntax


I've tried many different iterations of paths, but I always get one of two errors: 1, module not found, 2. Syntax error

I'm using Idle 2.7 and not sure what directory I am in.

How do I import myfile.py?
Last edited by Yoriz on Fri Dec 20, 2013 6:37 am, edited 1 time in total.
Reason: First post lock
AMD 4 Core CPU, UBUNTU 13.10, Python 2.7
Honduras2811
 
Posts: 6
Joined: Fri Dec 20, 2013 5:50 am
Location: Cabool, Missouri, USA

Re: Can't Figure Out How To Use A Path With The IMPORT Comma

Postby Yoriz » Fri Dec 20, 2013 6:39 am

Hi welcome to the forum.
There is a tutorial on the forum about modules
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 827
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Can't Figure Out How To Use A Path With The IMPORT Comma

Postby Honduras2811 » Fri Dec 20, 2013 7:53 am

So it seems that I need to do something like this:

Code: Select all
import sys
sys.path.append("/home/Rory/PythonWork")


Or is that too much path or too little?

Or is it a relative path? If it is relative, is it relative to the Python directory, or to the directory I am currently in?

If it is relative to the directory I am currently in, how do I find out what that is from inside Idle?

*** I just noticed that the answers to several oif my questions are in the Tutorials. I'm going to go through them before I ask any more questions.
AMD 4 Core CPU, UBUNTU 13.10, Python 2.7
Honduras2811
 
Posts: 6
Joined: Fri Dec 20, 2013 5:50 am
Location: Cabool, Missouri, USA

Re: Can't Figure Out How To Use A Path With The IMPORT Comma

Postby Mekire » Fri Dec 20, 2013 8:21 am

If you really want to import modules from inside subfolders you probably want to make the subfolders packages.

The sys.path.append solution is really just a hack. It shouldn't actually be included in production code (or code anyone else might look at).

If you put an empty file called __init__.py inside the subfolder, then you can import with

Code: Select all
from subfolder import module
in which case you would then access names in module with:
Code: Select all
module.name

or
Code: Select all
import subfolder.module
in which case you would then access names in module with:
Code: Select all
subfolder.module.name

Read more on packages.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Can't Figure Out How To Use A Path With The IMPORT Comma

Postby Honduras2811 » Fri Dec 20, 2013 9:59 am

My brain is fried. Too much input in a short time. :o

First, thanks, but there is still a disconnect somewhere.

You mention
Code: Select all
import subfolder.module


I tried this
Code: Select all
>>> import PythonWork.myfile.py

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    import PythonWork.myfile.py
ImportError: No module named PythonWork.myfile.py


At this point I see three things that I could have done wrong:
1. The '.py' extension. I think I read in one of the tutorials that you don't use the extension with import.
2. I think this part is analogus to your 'subfolder': PythonWork. So back to the questions about whether it is a relative or an absolute path.
3. I didn't put the _init_.py file in that folder or subdirectory.

I'm going to experiment with 1., I'll make the change in 3., but 2. remains due mainly to my unfamiliarity with Linux file systems. I noticed that Geany has a path browser built in. I'll see if that clears up my understanding of the pproblem.
AMD 4 Core CPU, UBUNTU 13.10, Python 2.7
Honduras2811
 
Posts: 6
Joined: Fri Dec 20, 2013 5:50 am
Location: Cabool, Missouri, USA

Re: Can't Figure Out How To Use A Path With The IMPORT Comma

Postby Mekire » Fri Dec 20, 2013 10:15 am

You never need .py with an import statement.
You always need an __init__.py file in the subfolder if you plan on importing it as a package.


The structure I'm talking about is this:
  • Folder that contains main script:
    • main.py
    • subfolder
      • __init__.py
      • some_module.py

The import statement inside main.py would look like this:
Code: Select all
from subfolder import some_module

You could then access names in some_module like this:
Code: Select all
some_module.name

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Can't Figure Out How To Use A Path With The IMPORT Comma

Postby metulburr » Fri Dec 20, 2013 11:50 am

Apparenty you did not fully read that tutorial Yoriz gave you, eh? Because it explicitly states halfway through part one that modules do not need the suffix .py
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Can't Figure Out How To Use A Path With The IMPORT Comma

Postby Honduras2811 » Sat Dec 21, 2013 12:15 pm

Ah, but I did read it. I should have left that out.

I FINALLY got it working using:

Code: Select all
sys.path.append("/home/rory/PythonWork")


Now everything works fine.

Things like this are really frustrating because it is the first step in using modules in the book. It's kind of as show stopper, too.

Thank you for your help and patience.

EDIT: How do I mark this thread as 'Resolved'?
AMD 4 Core CPU, UBUNTU 13.10, Python 2.7
Honduras2811
 
Posts: 6
Joined: Fri Dec 20, 2013 5:50 am
Location: Cabool, Missouri, USA

Re: Can't Figure Out How To Use A Path With The IMPORT Comma

Postby Honduras2811 » Sat Dec 21, 2013 12:28 pm

Mekire wrote:If you really want to import modules from inside subfolders you probably want to make the subfolders packages.

The sys.path.append solution is really just a hack. It shouldn't actually be included in production code (or code anyone else might look at).

If you put an empty file called __init__.py inside the subfolder, then you can import with

Code: Select all
from subfolder import module
in which case you would then access names in module with:
Code: Select all
module.name

or
Code: Select all
import subfolder.module
in which case you would then access names in module with:
Code: Select all
subfolder.module.name

Read more on packages.

-Mek


Neither of these methods worked for me. I keep getting a syntax error on the first '/' in the 'subfolder'.

EDIT: (For clarification) If I use '/home/rory/Pythonwork' The syntax error is on the first one
Same if I use 'home/rory/Pythonwork'.
Would it make a difference if I put it in quotes?
AMD 4 Core CPU, UBUNTU 13.10, Python 2.7
Honduras2811
 
Posts: 6
Joined: Fri Dec 20, 2013 5:50 am
Location: Cabool, Missouri, USA

Re: Can't Figure Out How To Use A Path With The IMPORT Comma

Postby Mekire » Sat Dec 21, 2013 2:16 pm

I'm afraid we are having a misunderstanding here.

If I'm not mistaken you want to be able to import modules from a specific directory whenever you invoke the interactive interpreter; regardless of what directory you invoke the interpreter from. You can do this by adding that folder to your path (though sys.path.append isn't the proper way to do this).

I on the other hand was trying to explain how to set up a package. This would involve creating a main script; and in a subdirectory within the same folder the main script was in (see previous post of mine) or a folder in your path, you would place your modules and a __init__.py file. You can not import a directory; there will never be slashes or quotes or .py in an import line. You can however import a package which basically amounts to the same thing. If this is too difficult you should just skip it for the time being. Many people don't learn how to use packages until quite a bit later.

Please look at my previous post again to see how the files are organized and make sure you realize that none of this has anything to do with the interactive interpreter. It has to do with executing a script. You can import packages in interactive mode but they will either need to be in a folder in your path, or in the current working directory.

Again, this is probably something for you to come back to later.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Can't Figure Out How To Use A Path With The IMPORT Comma

Postby metulburr » Sat Dec 21, 2013 2:18 pm

Neither of these methods worked for me. I keep getting a syntax error on the first '/' in the 'subfolder'.

EDIT: (For clarification) If I use '/home/rory/Pythonwork' The syntax error is on the first one
Same if I use 'home/rory/Pythonwork'.

In mekire's example you should not be using the character
Code: Select all
/

at all to define sub-directories. The main.py file in mekires structure example is the file executed. For simplicity, change directory to that main.py file and execute it The parent directories are irrelevant at that point. If there are sub-directories In import statements, they are periods not foreward slashes.

If you are still confused, then you can check out the example below. I did this all on the terminal so you can actually see what i write to the file. For quick rundown: mkdir creates a directory, touch creates a file, cat prints off the file content, ls lists the current directory content, echo "some_string" >> filename writes some_string to the file filename. At worst case scenerio you should be able to at least duplicate a working example with this, since your using linux.

this is an example all on the terminal:
Code: Select all
metulburr@arch ~ $ mkdir example
metulburr@arch ~ $ cd example
metulburr@arch ~/example $ touch main.py
metulburr@arch ~/example $ ls
main.py
metulburr@arch ~/example $ echo "from subfolder import module" >> main.py
metulburr@arch ~/example $ echo "print('running main')" >> main.py
metulburr@arch ~/example $ cat main.py
from subfolder import module
print('running main')
metulburr@arch ~/example $ mkdir subfolder
metulburr@arch ~/example $ ls
main.py  subfolder
metulburr@arch ~/example $ touch subfolder/__init__.py
metulburr@arch ~/example $ touch subfolder/module.py
metulburr@arch ~/example $ echo "print('running module')" >> subfolder/module.py
metulburr@arch ~/example $ cat subfolder/module.py
print('running module')
metulburr@arch ~/example $ python3 main.py
running module
running main
metulburr@arch ~/example $ python2 main.py
running module
running main
metulburr@arch ~/example $


and here is what each line is doing:
Code: Select all
create directory example in ~/
cd directory into example
create a new file called main.py
list files in directory (ls)
write to the file on first line in main.py "from subfolder import module"
write to the file on second line in main.py "print('running main')"
print the content of what is in main.py (cat)
make directory subfolder
list files and directories in current directory "example"
create a new file in  subfolder called __init__.py
create a new file in subfolder called module.py
write to the file on first line in the subfolder directory to file module.py
print the content of what is in subfolder/module.py
execute main.py via (python main.py) in the directory example
(code can be python2.x or 3.x)


the structure here is the same as mekires example
  • /home/username/example:
    • main.py
    • subfolder
      • __init__.py
      • module.py
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1469
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 7 guests