Running pyinstaller on code containing relative paths - python

I have a large python codebase developed inside PyCharm. I want to use PyInstaller for obvious reasons; however, I'm struggling with relative paths for data files due to the project code file hierarchy.
The file hierarchy was a usual top-down structure, i.e., the point of execution is within a file found in the project root folder, with the additional python files stored in a sensible folder, (please pay particular attention to the version.txt file on the same level as the Main.py file) e.g.,
Project/
--Main.py
--version.txt
--Engines/
----somefile.py
--Controllers/
----somefile.py
--Entities/
----somefile.py
A year ago, I built a GUI front end whilst maintaining the console-based point of execution. The GUI point of execution is within MainGUI.py. But that file is not at the project root. It looks a bit like this:
Project/
--Main.py
--version.txt
--GUI/
----MainGUI.py
--Engines/
----somefile.py
--Controllers/
----somefile.py
--Entities/
----somefile.py
Inside MainGUI.py, I have the code to open the "../version.txt" file:
with open("../version.txt") as file:
version = file.readline().strip()
I navigate to the Project/GUI folder in the PyCharm Terminal and execute pyinstaller MainGUI.py --onefile It seems to work until I try and execute the built MainGUI.exe. I'm given the error:
Traceback (most recent call last):
File "MainGUI.py", line 10, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '../version.txt'
[17232] Failed to execute script 'MainGUI' due to unhandled exception!
I could move the version.txt file to be on the same level as MainGUI.py, but this was a reduced example. There are lots of data files referenced using relative paths.
I would be grateful for any assistance. Thank you.

Related

python - error occurs on relative path but not absolute path in macOS

I'm having a strange issue. I'm just trying to do a basic show directory contents for relative path.
Created a test directory on the desktop
test directory contents
test.py
test1 -- folder
sample.txt
contents of test.py
import os
dataDir = "test1"
for file in os.listdir("/Users/username/Desktop/test"):
print(file)
Summary - absolute path
works - in visual studio code
works - macOS terminal python3 /Users/username/Desktop/test/test.py
however when use the variable I get an error:
contents of test.py
import os
dataDir = "test1"
for file in os.listdir(dataDir):
print(file)
Summary - relative path
works - in visual studio code
ERROR - macOS terminal python3 /Users/username/Desktop/test/test.py
Traceback (most recent call last):
File "/Users/username/Desktop/test/test.py", line 4, in
for file in os.listdir(dataDir):
FileNotFoundError: [Errno 2] No such file or directory: 'test1'
I all depends on what folder you are in when you launch the code.
Let's say your .py file is in folder1/folder2/file.py and your test1 folder is folder1/folder2/test1/.... You open a terminal in the folder1 and launch python3 /folder2/file.py. The program is going to check for files in folder1/test1/.
Try navigating to the actual file folder, that should work. If you want to use it wherever you launch the code you will have to do some checks on the current directory and manually point the code to the wanted path.
The following solution is inspired by that answer
A simple way to solve your problem is to create the absolute path. (I recommend using that always when you're working with different directories and files)
First of all, you need to care about your actual working directory. While using VS Code your working directory is in your desired directory (/Users/username/Desktop/test/).
But if you use the command line your actual working directory may change, depending where you're calling the script from.
To get the path where script is actually located, you can use the python variable __file__. __file__ is the full path to the directory where your script is located.
To use your script correctly and being able to call it using both ways, the following implementation can help you:
import os
dataDir = "test1"
# absolute path to the directory where your script is in
scriptDir = os.path.dirname(__file__)
# combining the path of your script and the 'searching' directory
absolutePath = os.path.join(scriptDir, dataDir)
for file in os.listdir(absolutePath):
print(file)

'No such file or directory' error when using nosetests

I have a little Python project of which I recently made a Conda package. Making the package was a pain on its own, however, I recently started adding tests to this using nosetests, which made it even harder.
To introduce: my package takes a certain input, performs a lot of (quantum chemical) calculations and then generates a folder in the same directory as the script which calls the package, containing the output data (some .png files, .txt files and binary files)
Using nosetests, I would like to check whether these output files are how they should be. I created a Python test script (using unittest) which creates the input and calls my package. Next, it imports the created file and the test file. However, this is where it goes wrong. I get the error that this file does not exist:
FileNotFoundError: [Errno 2] No such file or directory: 'results\\output.txt'
The directory looks like this:
project-path\
- tests\
- test-script.py
- results\
- output.txt
I call nose by running this in Anaconda prompt:
project-path> nosetests tests
And I import the file in the Python test script using:
result_file = open('results\\output.txt', 'r').read()
Does anyone know what goes wrong here? I think it has to do with the fact that the tests are executed in a test environment. In that case: how do I import my files?
Get the absolute path to output.txt, it is indeed the most reliable way to locate and open it.
import os, sys
basedir = os.path.dirname(sys.argv[0])
filename = "output.txt"
path = os.path.join(basedir, "results", filename)
result_file = open(path, 'r').read()

python code can't find file while its in folder marked as resource root in PyCharm

In PyCharm, I created folder named test, created txt file named test inside and marked folder as content root. When I added python file outside the test folder and tried to access test.txt file with open it suggested me file name. But when I tried running the code it couldn't find the file.
Traceback (most recent call last):
File "/home/elmo/PycharmProjects/TBC_PAY_API_TESTING/testing.py", line 32, in <module>
print(open("test.txt").read())
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
This is how the code and folders look like ( ingore the trash folder)
How can I fix it? The main reason I am doing this is to avoid writing full path's for accessing file.
You are talking about two totally different things in your question. If you mark a folder as Sources root that means the Python interpreter will be able to find the modules in that folder.
For example:
When you write an own module and you want to use it in another file the Python won't find it automatically. The PYTHONPATH should contain the path of the folder which contains your module. And actually the Sources root option does this!
The other thing what you have mentioned in your question is that you don't provided a correct path in your code. It is a real error. In your code, you have to provide the correct path for open. The Pycharm is an IDE but your (or other's) Python interpreter will use your code.
You can solve your problem in many ways.
For example:
You can hard-code the path of your txt (It is totally not recommended):
print(open("/home/elmo/PycharmProjects/TBC_PAY_API_TESTING/test/text.txt").read())
You can use relative path:
print(open("test/text.txt").read())
You can use full path based on your Python file (I recommend this solution):
import os
dir = os.path.realpath(os.path.dirname(__file__)) # Directory of your Python file
file_path = os.path.join(dir, "test", "test.txt") # Create the path of the file
print(open(file_path).read())

Call a file in another folder in Eclipse for Python project

I have a small enough Python project in Eclipse Neon and keep getting the same error and can't find proper documentation on how to solve. In my main I need to call a file that is located in another folder. The error I receive is IOError: [Errno 2] No such file or directory:
I have an empty init.py file in the folder (XML_TXT) that I'm trying to use.
It looks like Groovy is importing okay, or else you would get an ImportError. An IOError indicates that it can't find "test.txt". Does that file exist?
It will work if the file path is relative to where you are running the script from. So for example if test.txt is in a folder
Groovy("folder_name/test.txt")
You can also go up in the directory structure if you need to, for example
Groovy("../folder_name/test.txt")
Or, if you want to be able to run the file from anywhere, you can have python work out the absolute path of the file for you.
import os
filename = os.path.join(os.path.dirname(__file__), 'folder_name/test.txt')
u = Groovy(filename)

how do i get current working directory in atom

In atom, os.getcwd() always returns D:\WorkSpace\Test. So if I do things like open("01.txt"), it cannot find the file.
Also, this happens when using the "script" package to execute in Atom, But when executing the actual python file, it works.
I have found several other asking the same question, like this, but there is still no resolution.
Thanks to everyone who tried to help!
Added my directory:
D:\WorkSpace\Test
D:\WorkSpace\Test\01\01.py
D:\WorkSpace\Test\01\01.txt
or
D:\WorkSpace\Test
└─01
└─ 01.py
└─ 01.txt
Added my source:
01.py
import os
print os.getcwd()
f = open("01.txt")
print f.read()
01.txt
atom editor 01.txt
Added results(in atom):
D:\WorkSpace\Test
Traceback (most recent call last):
File "D:\WorkSpace\Test\01\01.py", line 5, in <module>
f = open("01.txt")
IOError: [Errno 2] No such file or directory: '01.txt'
Added results(in windows cmd):
D:\WorkSpace\Test\01>01.py
D:\WorkSpace\Test\01
atom editor 01.txt
In Mac, I solved this kind of issue by opening ATOM from a shell window, under target directory. Seems ATOM will use the directory it inherited from shell process as its working directory. You might try with a windows cmd window, see if it works.
Windows - go to Packages -> Settings View -> Manage Packages. Then go to System Settings on the left hand side menu and tick 'Show in file context menus'.
You can now go to your chosen directory and open any file (.js, .py etc.) with Atom and the current working directory will change to the one you chose instead of the default .atom.

Categories