How to specify a directory in Python [duplicate] - python

This question already has answers here:
How to reliably open a file in the same directory as the currently running script
(9 answers)
Closed 1 year ago.
I am making a simple game with pygame, and I want it to eventually be shareable with others. However, my code manually changes the directory of the python file each time, which uses a path. I thought just putting the code in a folder in file explorer would work to change the directory, but it did not, so I used os.chdir(r'C:\Users\lukep\Downloads\Gladiator'). This works great on my computer, but it won't on anyone else's. Is there a way to make sure my program is in the same directory as the images I need without making a specific file path? Any help is welcome. Thanks!

Hard to give a definitive answer without seeing your full code, however as a rule of thumb I would say try to make use of relative paths in your code.
Also, this SO question shows how to determine which directory your script is running in:
How do you properly determine the current script directory?
os.path.dirname(os.path.abspath(__file__))

Try making a new system path variable with the respective directory address
'C:\Users\lukep\Downloads\Gladiator'.
Go to System properties → Advanced system settings → Environment variable → Path and add new path variable by pasting 'C:\Users\lukep\Downloads\Gladiator'.

Related

PyCharm doesn't run files calles "main.py"? [duplicate]

This question already has an answer here:
How to run Main.py automatically in PyCharm
(1 answer)
Closed 1 year ago.
Sorry, this is driving me crazy. I just started using PyCharm, and have gone through the normal steps to create a Python project. PyCharm creates a default file called "main.py". However, PyCharm does not seem to know that this is a Python file, despite the ".py" extension in its name.
The text formatting color highlights in the text editor don't work.
There is no option to Run this file.
However - when I add a new Python file manually to this project with a different name, everything works fine. However, when I create a file called "main.py", nothing works, as described above. When I create a new Python file with a different name, and then rename it to "main.py", it doesn't work again.
Why is this happening? I've searched for this problem but haven't found an applicable solution. Why does PyCharm doing something weird to files called "main.py" specifically?
Thank you.
Try Ctrl+Shift+F10,
should automatically edit the configuration.
to add to the previous answer: you can also right click on the file and choose "Run 'main'", which automatically creates a configuration with the project interpreter for that file.
Another thing you can check is your interpreter settings and ideally make sure its using venv, this way packages you install don't go into global and are kept on a project basis.

How to get Blender script path that is running

How do I get the full path of the current file's directory?
Yes, I already tried these methods. They all give me the folder that contains Blender.exe
which is D:\program\Blender 2.90
And what I supposed to get is C:\Users\username\AppData\Roaming\Blender Foundation\Blender\2.90\scripts\addons\someAddonFolder right?
Is something changed in version 2.9?
Without the Blender environment, I mean run under normal Python. Methods from that topic all work well.
Did I miss something?
The old way I made the script work is by putting .blend .json .py all together in the same folder.
and refer to the relative path using //
bpy.utils.user_resource('SCRIPTS', "addons")
use this code can get you the path your custom addon you install at.
for me, it's 'C:\Users\Naoki\AppData\Roaming\Blender Foundation\Blender\2.90\scripts\addons'
found the answer over here:
https://blender.stackexchange.com/questions/20850/how-to-automatically-get-a-add-on-folders-path-regardless-of-os

How to deal with working on one project on different machines (paths)?

This is my first time coding a "project" (something more than solving exercises in single files). A number of my .py files have variables imported from a specific path. I also have a main "Run" file where I import things I've written in other files and execute the project as a whole.
Recently I've started working on this project on several different machines (home, work, laptop etc) and have just started learning how to use GitHub.
My question is, how do I deal with the fact that every time I open up my code on a different machine I need to go around changing all the paths to fit the new machine, and then change them back again when I'm home? I started writing a Run file for each location I work at so that my sys.path commands are ok with that machine, but it doesn't solve the problem of my other modules importing variables from specific paths that vary from machine to machine. Is there a way round this or is the problem in how I'm setting up the project itself?
In an ideal world it would all just work without me having to change something before I run, depending on the machine I'm working on, but I don't know if that's possible.
My current thoughts are whether there is some command I'm not aware of that can set variables inside a .py file from my main Run.py file - that way I can just have a run file for each machine.
Any suggestions are gladly taken! Whatever it is, it must be better than commenting back in the correct file path each time I open it on a different machine!
You should always use relative paths, not static which I assume you have got.
Assuming your in an index file and you need to access images folder, you probably have something like /users/username/project/images/image.png
Instead you want something like ../images/image.png, this tells your index file to go backwards one folder to say the root of the project, then proceed into our images folder etc.
Relative paths mean you create a path from where your file exists, and not an entire path from ground up.
You need to identify all your setting that are hardcoded in your project, and make them relative to your current workspace. This is a common problem.
Also if you are using python, make sure that you do not hardcode the path to files in string, but instead that you use the proper system api:
os.path.join('file','to','your','ressource')
that will be computed differently if you are on unix or windows.

How to have a dot in combination with an no file extension on Windows? [duplicate]

This question already has answers here:
How do I manually create a file with a . (dot) prefix in Windows? For example, .htaccess
(17 answers)
Closed 6 years ago.
I've a problem with a tutorial I follow from a book. I'm making a website with Django and Python. I'm now at the chapter where I export the website to an server.
I have to use Git in order to have some like a back-up for when anything goes wrong. Now I need to make a file, .gitignore, to let Git ignore some files. It must start with a dot and has no file extension.
I already knows how to make a file with no extension, to fill in the save field "filename". When I use ".gitignore" the file isn't visible, only the icon and nothing more.
I've tried it with another way, make a no file extension to type "gitignore", save it and then edit the name in .gitignore. But then Windows gives an error: "You must specify a filename"
It's about having a file with no extension beginning with a dot, I don't want to create a txt file.
Does anyone knows this problem and have a way to fix this?
Open git bash and form the terminal create the file. now you can edit it as well.

Starting a python script on boot (startx) with an absolute path, in which there are relative paths

I realise this question may already exist, but the answers I've found haven't worked and I have a slightly different setup.
I have a python file /home/pi/python_games/frontend.py that I am trying to start when lxde loads by placing #python /home/pi/python_games/frontend.py in /etc/xdg/lxsession/LXDE/autostart.
It doesn't run and there are no error messages.
When trying to run python /home/pi/python_games/frontend.py, python complains about not being able to find the files that are loaded using relative links eg: /home/pi/python_games/image.png is called with image.png. Obviously one solution would be to give these resources absolute paths, but the python program also calls other python programs in its directory that also have relative paths, and I don't want to go changing all them.
Anyone got any ideas?
Thanks
Tom
you could change your current working directory inside the script before you start calling your relative imports, use os.chdir("absolute path on where your script lives").
Rather than change your current working directory, in yourfrontend.pyscript you could use the value of the predefined__file__module attribute, which will be the absolute pathname of the script file, to determine absolute paths to the other files in the same directory.
Functions in theos.pathmodule, such assplit()andjoin(), will make doing this fairly easy.

Categories