LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 06-12-2018, 03:49 PM   #1
vysero
Member
 
Registered: May 2018
Posts: 32

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:

Code:
sys.path.append("../../../BuildTools")
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:

Code:
sys.path.append("..py0/Tools/")
What I tried to do was use os and say:

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

Code:
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
vysero
Member
 
Registered: May 2018
Posts: 32

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
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.4
Posts: 1,022

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

Original Poster
Rep: Reputation: Disabled
Cool

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

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


Reply


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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:08 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration