Relative Paths in different IDEs - python

lets assume following workspace is opened in an IDE:
myWorkspace
|- folder1
|- file1.py
|- folder2
|- file2.csv
I edit and execute file1.py and I want to use file2.csv in it. When I use PyCharm as an Editor, the correct way to do so is to use the path "../folder2/file2.csv". But when I use VSCode I need to use the path "folder2/file2.csv". So VSCode assumes the start to be the workspace folder, and it seems that my PyCharm Editor just takes the path where the file is executed as starting point.
My question: Which of these two ways is more common? What property causes such a behavior?

I would say that the more common way is the way VS code works. At least when running your program outside your IDE. The myWorkspace folder is your root folder for the project if I'm correct here.
But I don't see why you don't have anything in your root folder. Maybe you should create the main file for the entry point in your root folder and import the file from folder1.
This behavior is determined by how you execute your program.
If you run the script like this:
$ pwd
.../myworkplace
$ python folder1/file1.py
then the CWD (current working directory) will be the myworkplace folder and the folder2/file2.csv approach will work.
If you run the script like this:
$ pwd
.../myworkplace/folder1
$ python file1.py
then the CWD will be the folder1 folder and the ../folder2/file2.csv route will be the correct one.

Related

Have a question about python module import

Here is my project directory in Intellij
parent/
A/
test.py
In test.py, I have to import a module from parent level of parent folder.
import module_needed
but module_needed is in this structure:
parent_a/
parent_b/
py/
module_needed
application/
parent/
A/
test.py
I tried to add moduled_needed's path to sys.path in test.py. But still cannot find module.
Am I doing wrong? I am using Intellij, is this related to Intellij?
Python import system is quite straightforward. It looks for the packages in folders from sys.path. You have to add folder that contains module_needed in this case py (full path), not path to module_needed itself. If it's not there you can add the folder either from the code, directly appending to sys.path or with PYTHONPATH enviroment variable.
Also try printing sys.path, because Intellij might add project root to python path. Then the import statement would be:
import parent_a.parent_b.py.module_needed
without any modifications.
It might be useful to add the project root, then you won't have to add every single folder separately.

How to create subdirectories while saving Python files on Mac?

I'm learning Python using a book that uses Window's based examples and they use these as examples (the comments are the filenames):
# dir1\__init__.py
print('dir1 init')
x = 1
# dir1\dir2\__init__.py
print('dir1 init')
y = 2
# dir1\dir2\mod.py
print('in mod.py')
z = 3
Now using IDLE, it attempts to import the files using:
import dir1.dir2.mod
Which will display:
dir1 init
dir2 init
in mod.py
When I attempt to do this on IDLE on my Mac it comes up with an error saying there is no module named dir1.
The point of the exercise is to package imports but I don't if I'm formatting the files incorrectly or importing incorrectly.
I think the files are not in the same directory from where the IDLE is running or looking at.
Try to see where the IDLE is on your Mac, complete path. Once find that out may be try putting the files in the same place so that python can see those files.
As an experiment, I'd suggest, writing your scripts in an editor like VS Code. cd into the directory where you have saved the script. Run the script by python yourfilename.py and see what it says then. Personally, I have never used the IDLE but I have come across python not being able to find the file.
I'm not sure what exactly you are asking, but your file structure should look like this in Finder:
which is:
dir1
|-- __init__.py
|-- dir2
|-- __init__.py
|-- mod.py
And you need to make sure that your working directory in IDLE is the directory where dir1 is located. I don't think you can change that in IDLE, but you could do this to make it work:
>>> import sys
>>> sys.path.insert(0,'/path/to/directory/where/dir1/is/stored')

Run python script in package

I am struggling with running python script in shell. I use PyCharm where is everything ok, but I want to run script without running PyCharm.
So my project folder is like:
data/
file.txt
main/
__init__.py
script.py
tools/
__init__.py
my_strings.py
I want to run main/script.py, which start with from tools import my_strings and working directory should be data/.
My PyCharm config is:
Script path: <PROJECT>/main/script.py
Working directory: <PROJECT>/data
Add content roots to PYTHONPATH: YES
Add source roots to PYTHONPATH: YES
So I want to run main/script.py in shell on Ubuntu. I tried:
PYTHONPATH=<PROJECT>
cd <PROJECT>/data
python3 ../main/script.py
But I just got: ImportError: No module named 'tools'
Check out this post, it's explains the PYTHONPATH variable.
How to use PYTHONPATH and the documentation the answer points to https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH
When you run from the data directory by default python can't find your tools directory.
Also regarding your comment about needing to run from the data directory, you could just use open('../data/file.txt') if you decided to run from the main directory instead.
Ideally, you should be able to run your script from anywhere though. I find this snippet very useful os.path.dirname(sys.argv[0]). It returns the directory in which the script exists.
I simply forgot to export $PYTHONPATH as suggested by Steve.

How to properly import python modules from an adjacent folder?

I'm trying to follow Learn Python the Hard Way to teach myself python. I want to use the directory structure described in Exercise 46, which for my question I'll simplify down to this:
bin/
app.py
data/
__init__.py
foobar.py
In exercise 50, he says to start the program from the project's top level directory like this:
$ python bin/app.py
Afterwards stating that you start it from the top level directory so the script can access other resources in the project.
But I can't seem to import modules that are in the data folder from app.py. Am I misunderstanding how to setup the directory structure?
Edit: Here's the bare-bones setup I have to try and figure this out
In app.py I have:
import data.foobar
I have __init__.py in the data directory and foobar.py just contains some nonsense like:
class Test:
x = 0
The directory structure matches that of above.
I'm not sure what the exercise asks to do, but your top-level directory needs to be in the PYTHONPATH. Try:
$ export PYTHONPATH=$PYTHONPATH:$PWD
$ python bin/app.py

Python directory/module structure and importing

I'm trying to write a test for my python program, and I've created a /tests directory, where / means the projects root, not system root.
I have all my source files in /myProjectName.
Both directories have a __init__.py file, but project root does not, and both files are empty (do I need a __init__.py in the tests directory ?)
I've tried importing /myProjectName/main.py in /tests/test_main.py, but it doesn't work.
What is the right way to either structure the project directories or import main.py in test_main.py?
I would suggest moving your tests directory to be inside your project directory. Then you can use the answers here to import from the parent directory when you're in tests: Importing modules from parent folder
Otherwise, you can simply set your $PYTHONPATH to point to your project directory when you run tests.
I would like to suggest nose here as it automatically detects the tests and runs them for you
To run the tests, simply do a
$ nosetests
in your project directory
Why not try this line in testmain.py:
import os
os.chdir("/myProjectName/main.py ")
Then execute the script.
Hope it works!

Categories