0

I have a folder with the following layout:

root/
    package0/
       __init__.py
       main.py
    package1/
       __init__.py
       main.py

Inside the package1/main.pyI have import package0.

When I open a terminal on root folder and run python package1/main.py it works fine. But this is very strange since the cwd was not supposed to be included in path, only the folder in which the script is in, package1 in this case. When I print the sys.path I can see that the root folder is there.

When I run the same code on my other computer I get a import error as expected.

I cannot understand why I am seeing this behavior. I have already checked .bashrc and there is no code adding the cwd to the python path. What might be different on the two computers, I am transferring the root folder from one computer to another through git.

czr
  • 658
  • 3
  • 13
  • If they are truly separate packages, you probably want to _install_ "package0" into the development environment of "package1" and not keep them at the same level as is done here. If they don't need to be separate, just add a _parent package_ having "package0", and "package1" being sub-packages of the top-level _parent package_. – monkut Sep 17 '19 at 02:14
  • 1
    I agree with that. The point is that package1 is a folder with some command line utilities that are not supposed to be installed, they rely on package0. The point is I am getting a different behavior in two machines and I have no clue why. – czr Sep 17 '19 at 02:18
  • Ok, yes, the `cwd` should be dependent on where you executed the command. As @Enthus3d mentioned, _both_ packages should be in your PATH if you execute the command at the `root/` directory level. – monkut Sep 17 '19 at 02:34

2 Answers2

0

Essentially, whenever you start a particular script, the 'working directory' is the directory from which you started the script. When you run your script from the root folder using command line, the script will look for any files you mention with the root folder as the 'root' of any paths.

Hope that addressed some of your questions. If you're interested in working with changing the starting directory, you can read more about it here.

Edit: Continuation on how to solve changing the working directory for any particular file, this should grab your current running file's directory, change the path to it, and change the directory one higher.

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))
os.chdir(dir_path)
os.chdir("..")
Enthus3d
  • 1,727
  • 12
  • 26
  • Thank you, I am always running the scrip from the root folder. The problem is that in this computer, the cwd is in the sys.path always. On my other computer that does not happens, I get an import error, as I researched that should be the default behavior. Now I get code that runs differently on different computers so I don't know how to properly lay out the code. – czr Sep 17 '19 at 02:16
  • Ohh I see. I thought you were asking out of curiosity. So you wish to format the code so that it always runs properly, no matter where the script is run from? – Enthus3d Sep 17 '19 at 02:17
  • Let me try something out on my computer, I think I may be able to help you. – Enthus3d Sep 17 '19 at 02:19
  • Thank you very much. I want to have a repository that can be cloned, in this repository there is a package and some scripts in a separate folder that rely on this package. I want some one to be able to run those scripts without installing the package. The problem is that this is working the way it is on this computer. On the other I am getting import errors if I do not manually add the root folder to the path at the beginning of every script. – czr Sep 17 '19 at 02:22
  • One good way is to use `os.chdir('Path to your root directory')`, but I'm trying to figure out how we can do this for any user using your repository right now. – Enthus3d Sep 17 '19 at 02:28
  • I solved it by inserting `path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))` to the sys.path. But this does not solve the underlying problem of the cwd being added to my sys.path on my main computer. – czr Sep 17 '19 at 02:32
  • I had an idea, we can use your current file and find its path, using `dir_path = os.path.dirname(os.path.realpath(__file__))`, and then changing the directory to the root with that relative file. I would provide you some working code, but I'm trying to fix my compiler myself ;-;. – Enthus3d Sep 17 '19 at 02:33
  • @czr I changed my code a little more, added it to the bottom of my answer. This should change the working directory to be one folder higher than your current running script. Take a look? – Enthus3d Sep 17 '19 at 02:39
  • No problem. Hope it helps you fix the issue. :) – Enthus3d Sep 17 '19 at 02:54
0

I found a solution. I have no idea of why this solved the problem. I had two folders added to my pythonpath on .bashrc, some TensorFlow stuff, nothing related to this task. When I commented the line that added these folders, my cwd stopped to be added to the pythonpath. I took a look into those folders but I could not figure out what was causing this. I am still curious anyway.

czr
  • 658
  • 3
  • 13