LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-15-2016, 06:00 PM   #1
WildDrake!
Member
 
Registered: Dec 2003
Location: Current Location: Colorado
Distribution: Ubuntu 14.10, Mint 17.1 Cinnamon and Mate
Posts: 101

Rep: Reputation: 15
Smile Permanent Python Path


Linux Mint - Cinnamon
python 2.7.6 and Python 3.4.3
bash

I know this is basic...but...I am trying to get my python path set up so that I can import modules from directories other than the directory I am currently setting within.

I have set my my .bashrc file to include the following incantation:

export PYTHONPATH="${PYTHONPATH}:~/1_Scripts/python:~/1a_Computer_Related/Python/Practice:~/1a_Computer_Related/Python/code:~/1a_Computer_Related/Python/scripts:~/1a_Computer_Related/Python/Applied_Examples:~/python_work"

(My computer has been rebooted many times while trying to get this to work, so surely .bashrc is as read as it is ever going to get by the OS.)

However, so far I haven't gotten the interactive immediate mode of python (the console command line) to import anything from any of these directories. I have tried different incantations I googled, but to no avail...

pets.py is located in the last directory of the above path at ~/python_work :

>>> import pets
or
>>> from pets import Dog

Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named pets

What am I doing wrong??? Thanks for your thoughts!
 
Old 03-15-2016, 07:12 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
At your python prompt type the following and see if you can spot the problem:
Code:
>>> import sys
>>> print(sys.path)
 
Old 03-15-2016, 10:45 PM   #3
WildDrake!
Member
 
Registered: Dec 2003
Location: Current Location: Colorado
Distribution: Ubuntu 14.10, Mint 17.1 Cinnamon and Mate
Posts: 101

Original Poster
Rep: Reputation: 15
Apparently none of the paths for directories where I have placed python programs has been added to the actual path by the before mentioned statement! Below is the result I obtained with the sys.path command:

>>> import sys
>>> print(sys.path)
['', '/home/justme', '/usr/bin', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages']
>>>

So evidently none of the incantations I have found and tried so far are the correct statements....or .bashrc isn't where it wants me to say such things....like I have my Linux Path in the following text file /etc/environment instead of in .bashrc
 
Old 03-16-2016, 10:36 AM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Dunno. Works fine here.
Quote:
Originally Posted by WildDrake!
export
PYTHONPATH="${PYTHONPATH}:~/1_Scripts/python: ...
Probably a copy/paste issue, but are those separate lines in your .bashrc? They need to be on the same line or you need a backslash behind export.
Code:
export \
PYTHONPATH="${PYTHONPATH}:~/1_Scripts/python: ...
Once you get that working you're going to have problems using relative paths though.
 
Old 03-16-2016, 03:37 PM   #5
WildDrake!
Member
 
Registered: Dec 2003
Location: Current Location: Colorado
Distribution: Ubuntu 14.10, Mint 17.1 Cinnamon and Mate
Posts: 101

Original Poster
Rep: Reputation: 15
Hey! We are getting there! (If you haven't guessed...I am working at learning python.)

That seems to have been the problem...kind of. I changed the python relative paths to absolute paths in the .bashrc file, rebooted, and asked python:

Code:
>>> import sys
>>> print(sys.path)
['', '/home/justme', '/home/justme/1_Scripts/python', '/home/justme/1a_Computer_Related/Python/Practice', '/home/justme/1a_Computer_Related/Python/code', '/home/justme/1a_Computer_Related/Python/scripts', '/home/justme/1a_Computer_Related/Python/Applied_Examples', '/home/justme/python_work', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages']
>>>
So, the correct paths are now showing up as being active there.

Now, using the Linux Terminal in Python3 command mode to import from pets.py:

Code:
>>> 
>>> from pets import Pet, Dog
>>> polly = Pet("Polly", "Parrot")
>>> print(polly)
Polly is a Parrot
>>>
Hooray! It works!!!

But if I use the new Ninja python editor which I am trying to learn how to use to do this same thing it's python3 interactive window gives me:

Code:
>>> from pets import Pet, Dog
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named pets
>>>
Moving over to the Python3 Idle interactive shell for beginners I get the same error:

Code:
>>> from pets import Pet, Dog
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    from pets import Pet, Dog
ImportError: No module named 'pets'
>>>
The geany editor in python immediate mode also gives the same "No module named 'pets'" error.

Any suggestions or ideas of why these editor's python immediate mode windows aren't working correctly now?
 
Old 03-16-2016, 03:56 PM   #6
WildDrake!
Member
 
Registered: Dec 2003
Location: Current Location: Colorado
Distribution: Ubuntu 14.10, Mint 17.1 Cinnamon and Mate
Posts: 101

Original Poster
Rep: Reputation: 15
Just realized I hadn't check sys.path from any of the prior mentioned editors.

They do not show any of my personal directories on their sys.path printouts like the standard linux terminal running the python shell did...
 
Old 03-16-2016, 04:12 PM   #7
WildDrake!
Member
 
Registered: Dec 2003
Location: Current Location: Colorado
Distribution: Ubuntu 14.10, Mint 17.1 Cinnamon and Mate
Posts: 101

Original Poster
Rep: Reputation: 15
If I launch Ninja using the desktop launcher, I get the described not found problem. HOWEVER, if I launch Ninja-IDE from the linux command line, the Ninja editor then has the entire python path info and imports work from it's python commandline.

I assume that is what needs to also happen to get the other editors I mentioned to perform correctly also.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Installation of python and dependencies in customized path instead of standard path srinietrx Linux - Software 2 06-01-2015 09:19 AM
Slackware- Help installing a permanent path PeterUK Slackware 13 04-30-2013 12:14 PM
Permanent $PATH edits x81kilo Linux - Newbie 2 09-20-2010 05:34 PM
$CFLAGS and $PATH - how to change them so that they stay permanent kiwi_MTBer Linux - Newbie 8 04-16-2009 09:51 PM
Permanent PATH variable with azureus dreamcatcheromerta Linux - Newbie 4 11-06-2008 07:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration