Python: Having difficulty obtaining current path of script [duplicate] - python

This question already has answers here:
How do I get the path of the Python script I am running in? [duplicate]
(5 answers)
Closed 7 years ago.
I'm very new to programming, and to Python. I'm on Mac OSX, trying to work with PyCharm. I've looked at this, this, this, and several more.
But I can't seem to get the path of the current running file. If I use os.getcwd(), I get
'/Users/AlanH/Python'
When really, the full file path is:
'/Users/AlanH/Python/Exercises/PythonBasics/starthere.py'
So I don't understand why that doesn't work.
If I try using sys.path[0], it prings up a path that takes me to my Library, then dives in to Enthought (don't know why, even though I'm using Anaconda distribution).
I could go on and on about all the possible solutions I've tried, but nothing works. All I want is to get the exact path up to current running file. So either
'/Users/AlanH/Python/Exercises/PythonBasics/starthere.py'
or this
'/Users/AlanH/Python/Exercises/PythonBasics'
will do.
Could someone please help?

import os
print(os.path.abspath(__file__))
Locally tested with the following result:
eb#cube ~/Share $ python stackoverflow/path.py
/home/eb/Share/stackoverflow/path.py

Related

Python: Running Code from Notepad++ to the Python Console [duplicate]

This question already has answers here:
How do you run a python script from within notepad++? [duplicate]
(6 answers)
Closed 6 years ago.
I'm attempting a small coding project; a simple encryption program. I was wondering if I would be able to directly run my Python code from Notepad++, or if there is a different program that could do this task (preferably free). Not that I have to have said feature, it would just make things easier for me.
Sorry if this was an off-topic question. If so, I sincerely apologize.
Thanks in advance!
Wing IDE and PyCharm (My favourite personally) are really powerful IDE's that can run and debug your application all inside the same environment. Alongside features like auto-completion, support for documentation, version control support right out of the box, and multiple build configurations (there's way more features to list here!), they make writing and testing code more efficient.
Otherwise, you can always run your code via a shell, though the back and forth makes workflow less efficient.
The community edition is free, and very powerful still:
https://www.jetbrains.com/pycharm/download/
If you want a text editor that will run the code directly, then try PyCharm.
Otherwise open up a command prompt, save the file (e.g. simplencrypt.py) and then run python.exe simplencrypt.py in the command prompt when you want to run it.
I don't think you will be able to run your code directly from Notepad++.

How to turn a python program to a .exe [duplicate]

This question already has answers here:
Process to convert simple Python script into Windows executable [duplicate]
(8 answers)
Closed 9 years ago.
I have been working a lot on python recently, mostly using IDE. Now I have a need to make a .exe program out of my code. Have tried cx_freeze but i couldn't understand what to do. So, if anyone could either give me a link to a good guide for begginers, or another easier .py to .exe program, I would be grateful.
PS
I am using Python 3.3.
Try py2exe..
Install py2exe in your system, then generate a setup file as shown here
Thats it. Your .exe file will be created.

How can I compile a Pygame program to a Windows .exe [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I make an EXE file from a Python program?
I have found several links talking about what to do, but I am unsure of how to use them. They often say to just use the code, but they don't say if I should put it in the terminal or use it to make a program to run. Many recommend the use of py2exe but when I try to use it it will not run because it says I do not have python 2.6 in my registery. I am rather new to the more complicated side of programming and any help would be appreciated. I am running windows vista.
This is certainly a duplicate question, but I'd recommend using py2exe. We probably need more information on how or why you are failing.

Are cx-freezed executables hackable [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can EXE generated by cx_freeze be completely decompiled back to readable Python code?
I've used cx-freeze to convert a python script to an executable. Is it possible to reverse that? Can I get my python script from the binary cx-freeze has generated?
EDIT. This is a practical question. I really need to recover one cx-freezed executable to a more-less readable form. Does anyone have a recipie of doing it?
Any 'frozen' PY application can be disassembled but not really decompiled. With enough time and patience someone would be able to reverse-engineer nearly any program (including yours).
Edit
Correction as per comments below.

How to package a python3K application to an executable? [duplicate]

This question already has answers here:
Py2exe for Python 3.0
(5 answers)
Closed 8 years ago.
I would like to deliver the application to my client, who don't have any computer knowledge, therefore, I don't wanna to let him/her to use terminal to execute my script. How can I make a single package, and let the user double click and execute?? Is there any way to do so??
Moreover, is this possible to package the python runtime to the application? Because the client may not wanna to install python.... I just wanna him/her running something like a .exe written from C, just double click and execute it. Any tools to do so?? Thanks.
Please recommend for Windows & Mac OS X.
Py2Exe for Windows:
http://www.py2exe.org/index.cgi/FAQ
And a brief Google leads me to Py2App for OS X:
http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html

Categories