1.11M Members

python- creating modules

 
0
 

well this is the first time i am trying to make a module, i saved the .py file into a new directory called MyModules and tried to import it in IDLE, but it returned the following error;

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

i found some advice about using the linux terminal and a command called setenv, but it says it doesn't exist. i know this is a really stupid question but i cant find an answer, sorry and thank you for any help.

 
0
 

If you want to prepare package called MyModules, and the parent directory is in your PATH or PYTHONPATH, then you should create an empty file called __init___.py in the directory and import the module as MyModules.polymod.

If you want to import all your modules from this directory and they are unrelated, you could add the directory to your PYTHONPATH or PATH. If you add it to your path it is simple to start the script anywhere, notice though that the current directory will then point to the directory you start the script from, not the directory of the script.

The most simple way to import stuff is not to separate the module in subdirectory, but have a directory say 'polynoms' or 'pythonprograms', which contain both the main file of the program and the modules. Then you simply CD to the directory before starting the program and importing should succeed from current directory also, so it does not need to be added to PATH or PYTHONPATH.

http://docs.python.org/tutorial/modules.html#the-module-search-path

 
0
 

If you want to prepare package called MyModules, and the parent directory is in your PATH or PYTHONPATH, then you should create an empty file called __init___.py in the directory and import the module as MyModules.polymod.

If you want to import all your modules from this directory and they are unrelated, you could add the directory to your PYTHONPATH or PATH. If you add it to your path it is simple to start the script anywhere, notice though that the current directory will then point to the directory you start the script from, not the directory of the script.

The most simple way to import stuff is not to separate the module in subdirectory, but have a directory say 'polynoms' or 'pythonprograms', which contain both the main file of the program and the modules. Then you simply CD to the directory before starting the program and importing should succeed from current directory also, so it does not need to be added to PATH or PYTHONPATH.

http://docs.python.org/tutorial/modules.html#the-module-search-path

how do i add the directory to my PYTHOPATH?
thank you

 
0
 

Add the following line to your file ~/.bashrc

export PYTHONPATH="$HOME/MyModules:$HOME/fooo/bar/baz/qux/someotherdirifyouwant"

then restart a bash shell (or a terminal). You don't need an __init__.py in MyModules because it is not a python package.

 
0
 

Add the following line to your file ~/.bashrc

export PYTHONPATH="$HOME/MyModules:$HOME/fooo/bar/baz/qux/someotherdirifyouwant"

then restart a bash shell (or a terminal). You don't need an __init__.py in MyModules because it is not a python package.

im sorry i dont understand, could you explain, then i will try it again?

 
0
 

im sorry i dont understand, could you explain, then i will try it again?

If you are in linux, your home directory contains a file named '.bashrc' (notice the dot). Type ls ~/.bashrc or ls -a ~ in a terminal to check. Then open this file with your favorite editor and add the line

export PYTHONPATH="/path/to/your/directory/MyModules"

then save the file and exit the editor, and type 'bash' in your terminal to restart the shell. If your python IDE is opened, you need to restart it as well.

 
0
 

If you are in linux, your home directory contains a file named '.bashrc' (notice the dot). Type ls ~/.bashrc or ls -a ~ in a terminal to check. Then open this file with your favorite editor and add the line

export PYTHONPATH="/path/to/your/directory/MyModules"

then save the file and exit the editor, and type 'bash' in your terminal to restart the shell. If your python IDE is opened, you need to restart it as well.

how do i open bashrc? i tried to in the terminal but it said permission denied?!

 
0
 

how do i open bashrc? i tried to in the terminal but it said permission denied?!

if your desktop is gnome, type

cd
gedit .bashrc

in the terminal. If your desktop is KDE, type

cd
kwrite .bashrc
 
0
 

if your desktop is gnome, type

cd
gedit .bashrc

in the terminal. If your desktop is KDE, type

cd
kwrite .bashrc

i did as you said but it still wont load the module in IDLE. im going to have to do the rest tomorrow, its almost 7am. thank you for the help though, much appreciated

 
0
 

You may need to restart your session. If you want, you can post a part of your .bashrc to see if it's OK.

 
0
 

You may need to restart your session. If you want, you can post a part of your .bashrc to see if it's OK.

thanks for your help, when i started up today i created a new directory on my desktop with the name MyModules, worked immediately. i get the feeling there was something more t the problems i was having yesterday, the name of the directory i had My_Modules in last night doesn't come up right in the terminal and wont open when i put in the name as it appears or as it actually is, strange. but now i have a new directory and it works properly, thank you for your time.

seems i was mistaken.. i just restarted python and tried again, didn't work again?! i opened the module file and ran it, then tried import and it worked, but this is obviously not going to be of much use. i dont understand why it wont work.

 
0
 

thanks for your help, when i started up today i created a new directory on my desktop with the name MyModules, worked immediately. i get the feeling there was something more t the problems i was having yesterday, the name of the directory i had My_Modules in last night doesn't come up right in the terminal and wont open when i put in the name as it appears or as it actually is, strange. but now i have a new directory and it works properly, thank you for your time.

seems i was mistaken.. i just restarted python and tried again, didn't work again?! i opened the module file and ran it, then tried import and it worked, but this is obviously not going to be of much use. i dont understand why it wont work.

It's impossible, the PYTHONPATH must work. Can you post your ~/.bashrc ?

 
0
 

It's impossible, the PYTHONPATH must work. Can you post your ~/.bashrc ?

here it is, i added the line to the bottom of the file, saved it, the ran the 'bash' command in the terminal.

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

export PYTHONPATH="/home/wolf/Desktop/MyModules"
 
0
 

If you changed the ~/.bashrc since you logged in, did you start python from the terminal where you ran the bash command after changing .bashrc ?

You can also type 'echo $PYTHONPATH' in the terminal to check that the pythonpath is correct.

 
0
 

If you changed the ~/.bashrc since you logged in, did you start python from the terminal where you ran the bash command after changing .bashrc ?

no i start python using synapse, should i be starting python from the terminal?

the python path is as it should be

wolf@p:~$ echo $PYTHONPATH
/home/wolf/Desktop/MyModules

 
0
 

no i start python using synapse, should i be starting python from the terminal?

the python path is as it should be

wolf@p:~$ echo $PYTHONPATH
/home/wolf/Desktop/MyModules

Normally no, you don't need to start python from the terminal, but if you changed your .bashrc since the last login, it may be different. Does it work in the terminal ?

You can also check this from python to see if PYTHONPATH worked

>>> import os
>>> print os.environ["PYTHONPATH"]

and also this, to see if MyModules is on the path

import sys
print sys.path
 
0
 

Normally no, you don't need to start python from the terminal, but if you changed your .bashrc since the last login, it may be different. Does it work in the terminal ?

You can also check this from python to see if PYTHONPATH worked

>>> import os
>>> print os.environ["PYTHONPATH"]

and also this, to see if MyModules is on the path

import sys
print sys.path

i tried the os pythonpath thing, but it returned as follows;

print os.environ["PYTHONPATH"]
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    print os.environ["PYTHONPATH"]
  File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)
KeyError: 'PYTHONPATH'

im going to restart my machine and try again.

 
0
 

You can also try

python -c "import polymod"

in the terminal to see if it complains.

 
-1
 

Great information! I’ve been looking for something like this for a while now. Thanks!

 
0
 

You can also try

python -c "import polymod"

in the terminal to see if it complains.

i ran the command, didn't do anything. after restarting my machine python still doesn't import polymod. according to the terminal the PYTHONPATH is set right, so why wont it work? i dont get it

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: