To avoid type in long path name, I am trying to create a folder to put all my .py file in. And I want it to be some sort of "default" folder that every time I run .py file, the system will search this folder to look for that file.
One solution i figured, is to put my .py file in those module folders like "python\lib", and I can call python -m filename.
But I do not want to make a mess in the lib folder.
Is there any other ways to do it? Thanks!
I'm assuming you're running Python on Windows (the '\' backslash is my only clue). If so, I think you've got at least one reasonable option.
Create a python_run.bat file similar to this:
#ECHO OFF
REM *** MODIFY THE NEXT LINE TO SPECIFY THE LOCATION OF YOUR SCRIPTS ***
SET SCRIPT_DIR=C:\Path\To\Scripts
REM *** MODIFY THE NEXT LINE TO SPECIFY THE LOCATION OF YOUR PYTHON.EXE ***
SET PYTHON_BIN=C:\Python27\python.exe
PUSHD %SCRIPT_DIR%
%PYTHON_BIN% %*
POPD
Then make sure the folder where the python_run.bat is located is in your PATH environment variable. So if the script lives in C:\Path\To\Scripts\python_run.bat, you'd make sure your PATH environment variable had C:\Path\To\Scripts in it.
Then you simply have to type the following to execute any script located in your SCRIPT_DIR.
python_run my_cool_script.py --foo=bar
And it will result in running the following command as if you were already inside your scripts folder:
C:\Python27\python.exe my_cool_script.py --foo=bar
for example: first type
sys.path.append("/home/xxx/your_python_folder/")
then you can import your own .py file
It's not possible to do that without path. The only thing that you can is putting all of modules that you want to use in the same directory, you don't have to put them python\lib , you can put them in a folder on your desktop for example.Then run your scripts in that folder but, always be sure starting scripts with #!/usr/bin/env python.
Related
Now I am running the python file everytime like this:
python C:/my/path/to/python/file.py
I want to replace it with one word command like this
myfile
The same command should work from command_prompt, powershell and powershell_ise and it should work from any directory that I am in.
If you made the python an exe and cd into the directory or add it to your environment variables you could call it by myfile or myfile.exe
Another way would be to make a batch file that contains
python C:/my/path/to/python/file.py
and name it myfile which would allow you to call the batch file and it would type out the rest
Also if you cd into C:/my/path/to/python
you could type python file.py instead of the whole path.
You can basically create Custom Environment Variables. This link will guide how can you do that.
Link: Create Custom Environment Variables in Windows
The file option is interesting because it means you can also create an environment variable to launch a program. For example, you can point an environment variable to any EXE file on your system. When you invoke the variable, it will launch the program. If you have a custom executable program file stored in some random directory on your PC, this is an easy way to launch it without having to go look for it.
Or if you want to run your python script from anywhere, technically not anywhere but from other directory also then you can create a .bat file. And opening that .bat will automatically run your python script. You can create a .bat by :
Right click create a new file. Name it as script.bat.
Right click on script.bat and open it in Notepad.
Copy/paste this script python C:/my/path/to/python/file.py
Save it and run that script.bat.
It will automatically run you file.py from here. You can place script.bat in your preferred place like in desktop.
I have a python executable I wish to run from PowerShell using the command python [executable].py.
First I changed the directory in PowerShell to the location of the executable using cd path\to\my\directory which worked fine. However whenever I tried to use python to execute my code, PowerShell immediately searches for the [executable].py in Python's installation folder - fails to find it - and gives the an error that it cannot find the appropriate file.
How do I make sure that Powershell looks for the executable in the directory I indicated as opposed to the default Python installation folder?
If you want to run python.exe from a location other than the installation directory you'd call it with its full path:
& 'C:\path\to\python.exe' 'your.py'
If you want to run it from the current directory, prepend the filename with the relative path .\:
& .\python.exe 'your.py'
If you call an executable without a path like this:
& python.exe 'your.py'
PowerShell will look for a matching file in the directories listed in the $env:PATH environment variable, and execute the first match (or report an error if no matching file can be found).
With that said, the error you got in your screenshot is not because of the Python interpreter, but because of the file you want the interpreter to run. You're calling
python conditions
when you actually want to run
python conditions.py
Neither PowerShell nor Python magically add the extension for you. Instead they report an error because a file conditions (without an extension) simply doesn't exist.
I am a Python rookie and I learn Python3.3 in Windows.
I wrote my first script, a script to rename files with incremental filenames. But by now, I had to copy my rename.py script to the directory I want to change the files names and run Python rename.py. It is not a very fancy way to use.
I can improve my code and pass the target directory to run my script in origin directory like Python rename.py .../TargetDir, but I have to copy the directory everytime.
So I want to make my script a system command, then I would only type rename in cmd.exe in the directory I want to rename a bunch of files. How can I approach this.
For this purpose, you'll want to use doskey, which allows you to set aliases for commands.
You use it like this:
doskey macroName=macroDefinition
So you would want to write something like this:
doskey rename=Python rename.py .
Where the . stands for the directory you're currently in. (I wasn't exactly clear on what you wanted -- the way I read your question was that you just want to cd into the directory where you want to rename a bunch of files, then run the script.)
Use sys.argv to get the command line arguments. For example test.py:
import os
import sys
path = sys.argv[1]
print(os.listdir(path))
and then you can create a batch file which should placed in a folder that belongs to the PATH variable. In order to do so, create a text document with the following contents and save it as ListDir.bat. Copy the ListDir.bat to either your python folder, or Windows folder (both should be in your PATH)
ListDir.bat:
python C:\test.py "%CD%"
PAUSE
The %CD% refers to the current directory in the windows prompt. So assuming the python script test.py is in C:\ the first line executes the test.py script with the argument current directory.
I used PAUSE to get user input before completing the script, you could choose not to.
After you save the ListDir.bat file. You can navigate to the folder you want to use it in, and just call ListDir
I just downloaded Python 3.2 to Mac OS 10.6 environment. I'm new to programming and am trying to run my first stand-alone .py file, but I keep getting an error message saying "no such directory or file." The name of the file is "script1.py" and I saved it to /Users/billp/Documents. When I open the Terminal to run the file I type:
python script1.py
I've also tried adding this line to the beginning of the script:
#!/usr/local/bin/python
As well as this one:
#!/usr/bin/env python
Yet, I keep getting the same error message. Any help would be greatly appreciated.
Make sure you are in the right working directory after opening terminal. Type
cd /Users/billp/Documents/
(use tab to autocomplete)
then
python ./script1.py
This way you are launching python executable and passing it path to your file as the first argument. The shebang #! line you mentioned allows you to launch your script directly, like this: ./script1.py, but you need to mark the file as executable chmod +x script1.py and provide path to interpreter (python) after the shebang. #!/usr/bin/env python references your default python installation.
The ./ stands for current directory. By default when you type script1.py your shell (which is the thing that you type commands into, through the terminal) would look for executable file in special folders listed in PATH env variable. script1.py is not usually there, so you would see -bash: script1.py: command not found. python, on the other hand is on the PATH so it should work.
Maybe you forgot to make the file executable? Try this at the command prompt:
$ chmod +x script1.py
I prefer to start my Python scripts in a Mac with these lines (assuming of course that you're saving the file in UTF-8 encoding:
#!/usr/bin/env python
#coding=utf-8
Also, make sure that the pythoncommand is available in the path. If everything is set up correctly, it won't be necessary to type python first, and you can run the script directly by typing ./script1.py in the directory where it is located.
One final thing, for running a piece of code when executing the script from the command line (as opposed to simply loading the definitions in the file), write this at the end:
if __name__ == '__main__':
# the code you want to call
Are your python binaries here?
/Library/Frameworks/Python.framework/Versions/3.2/bin/python
It's worth pointing out, as long as the file is in your current directory it's automatically available. Otherwise, anything files will have to be referenced absolutely using the full path information.
So the following examples are calling the same file:
Explicit (absolute path)
python /Users/billp/Documents/script1.py
python /Users/billp/Documents/script2.py
python /Users/billp/Documents/script3.py
Implicit (relative path)
cd /Users/billp/Documents
python script1.py
python script2.py
python script3.py
As long as you're working with files in the same directory (commonly known as your working directory), you may always safely use relative paths. If the files are anywhere else, you must always specify an absolute path.
how can i run my program using test files on my desktop without typing in the specific pathname. I just want to be able to type the file name and continue on with my program. Since i want to be able to send it to a friend and not needing for him to change the path rather just read the exact same file that he has on his desktop.
f = open(os.path.join(os.environ['USERPROFILE'], 'DESKTOP', my_filename))
If you are on Unix/Mac you can add a shebang at the top of your script and set it as executable. Usually you'd do:
#!/usr/bin/env python
And then to make it executable, from a terminal use chmod:
chmod +x script.py
Now you can run the script (if you are in the same directory) like:
./script.py
If you are on Windows you'll want to add the Python executable to your %PATH% enviromental variable, then you can run your script with python script.py. They talk about this in more depth in the Python documentation: http://docs.python.org/using/windows.html
If you place your Python script in the same directory as the files your script is going to open, then you don't need to specify any paths. Be sure to allow the Python installer to "Register Extensions", so Python is called when you double-click on a Python script.
You can tell your friend to make *.py files to be executed by the interpreter. Change it from Explorer:Tools:Folder Options:File Types.