Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-12-2018, 03:49 PM   #1
Registered: May 2018
Posts: 104

Rep: Reputation: Disabled
os.path and sys.path.append

So here is my problem. I have a script located in a directory, call it x. From that script I am attempting to import a module I have created which is located in a different directory. I can access that directory just fine by saying:

The problem I am having is that the module located in the BuildTools directory is also attempting to append a path to access a different module, namely:

What I tried to do was use os and say:

z = os.path.abspath(__file__)
then simply change it to:

sys.path.append(z + "../py0/Tools/")
This isn't working. I have figured it out using a different method but what I want to know is why I can't do it like this, any ideas?
Old 06-12-2018, 03:55 PM   #2
Registered: May 2018
Posts: 104

Original Poster
Rep: Reputation: Disabled
Wow, funny how just typing out a problem can help you solve it. What I needed to do was first change to directory z then append.
Old 06-12-2018, 04:09 PM   #3
Senior Member
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 1,807

Rep: Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566
Alternately, always use absolute paths in scripts.
Old 06-12-2018, 04:37 PM   #4
Registered: May 2018
Posts: 104

Original Poster
Rep: Reputation: Disabled

Yes, also I figured out there is an os.path.join() function.
Old 06-12-2018, 07:27 PM   #5
LQ Guru
Registered: Sep 2013
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 6,950

Rep: Reputation: 1400Reputation: 1400Reputation: 1400Reputation: 1400Reputation: 1400Reputation: 1400Reputation: 1400Reputation: 1400Reputation: 1400Reputation: 1400
knowing the api helps, what helps more is good documentation on the api that is easy to get around in.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LXer: How to change sys.path or PYTHONPATH in Python LXer Syndicated Linux News 0 12-09-2015 04:33 AM
Moving files from parent path to multiple child path using bash in efficient way Karthikgv417 Programming 5 05-03-2015 10:05 PM
what is this path /sys/block/ram jnreddy Linux - Server 3 03-13-2012 08:21 AM
Append the windows path with Samba login script ?? amitsharma_26 Linux - Networking 2 09-29-2005 10:46 AM
How to Chnage Python's module search path (sys.path)? lramos85 Linux - Software 1 05-02-2004 06:10 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:20 AM.

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