Setting up default pylint config.rc file in Windows - python

I'm using Pylint under Windows, and it's not reading my pylint-config.rc file.
Is there a way to set up a default .rc file for Python within windows so that I don't have to keep typing it into the command line?
Thanks.

I don't have a windows box at hand to test, but the code uses os.path.expanduser('~') to find the current user's home directory, and looks for a file calle .pylintrc in that directory.
According to the python documentation, on Windows, expanduser uses HOME and USERPROFILE if set, otherwise a combination of HOMEPATH and HOMEDRIVE. So my advice is to check in a Python interactive session what the following script outputs:
import os
print os.path.expanduser('~')
and put the configuration file as .pylintrc in that folder.
Alternatively, if you want to use different configuration files on a per project basis, you should know that if there is a file called pylintrc (without a leading dot) in the current working directory, then Pylint will use this one. If there is a file called __init__.py in the current working directory, Pylint will look in the parent directory until there is no such file and then look for a pylintrc configuration file. This is done so that you can maintain a per project config file together with you source code, and lauch Pylint from any directory in your source tree.

Since creating a file beginning with a dot is not allowed from Windows file explorer, you can create a template using:
pylint --generate-rcfile > .pylintrc

There are two possible ways to do this. One way is to edit the file C:\Python\Scripts\pylint.bat changing the line
python "%~dpn0" %*
to
python "%~dpn0" %* --rcfile="C:\path\to\pylint.rc"
Another way is to go to add an environment variable. Do this by going to Start->Control Panel->System then going to the Advanced tab and clicking Environment Variables. Then click New and create a variable named PYLINTRC with the value of C:\path\to\pylint.rc.

Related

Wrong current working directory when running python code and jupyter extension in vscode

I am trying to run python code using the jupyter extension in vscode here
I have a workspace open at this location
/Users/user/Documents/
When I try and run the following code in a file called test.py in a child directory, the current working directory is set at the workspace level rather than the file. Is it possible to change a setting to use the cwd of the file, rather than the workspace? I cannot find one in settings.json and the "cwd" in launch.json only seems applicable to debugging.
File location:
/Users/user/Documents/python_code/test.py
#%%
import os
print(os.getcwd())
Expected output:
/Users/user/Documents/python_code/
Actual output:
/Users/user/Documents/
When running the same code through the terminal it prints the expected result, so the issue seems to be related to the jupyter extension
For any specific folder / workspace that you have open in VS Code you can use the notebookFileRoot setting to set a specific absolute path directory to always set the Jupyter working directory to when you start the Interactive Window with that folder open.
Always opening on the file location (without having to set notebookFileRoot to an absolute path per folder) is not supported via the notebookFileRoot setting. The VSCode variables such as ${fileDirname} are specific to task and debug configuration files (launch.json and task.json). We've specifically added code to recognize ${workspaceFolder} for our settings page but we don't recognize other VSCode variables in there.
If you want there is a github item here that has suggested this feature. You can follow that or vote it up if you want this feature added.
https://github.com/Microsoft/vscode-python/issues/4441
Edit 11/5/2019:
We have now made a change to allow the VS Code "Notebook File Root" setting to be set to ${fileDirname}. This setting will now start up Notebook Editor and Interactive Window session relative to the file location that was used to start them.
There is the setting python.dataScience.notebookFileRoot which is, as far as I understand, supposed to achieve the expected behaviour when setting it to ${fileDirname}.
See correspoding source.
However, it does not seem to work in my case. Maybe a bug?
Note that the output of running the script from the terminal depends on you working directory of the terminal!

Cannot import module - why does sys.path not find PYTHONPATH?

I am running Python 3.6.1 on a Windows 7 machine. I have some scripts in H:\Myname\Python\myscripts.
I have created the user variable PYTHONPATH and set it to the folder above. I do not have admin rights so I can create a user variable only.
In that folder, I have a myscripts.py file with some functions.
If I try to access it running import myscripts from a file stored elsewhere, it doesn't work: I get a ModuleNotFoundError
If I print sys.path, the folder I have set in PYTHONPATH is not there.
Why? What am I doing wrong? Isn't sys.path supposed to show PYTHONPATH?
Does the fact that H is a network drive have anything to do with it?
I can't seem to find anything on the web for this problem in relation to Windows (but lots for Unix systems).
A common way to fix this quickly is to use
sys.path.append("path/to/module")
Be careful with '\\' if you are using Windows.
Not exactly answering your question but this could fix the problem.
It is likely that there is a ._pth file overriding the default behavior of sys.path. Remove or rename the python._pth (or python36._pth) file.
There was a python37._pth file included in my download of the Windows embedded zip file that caused sys.path to not include PYTHONPATH.
To completely override sys.path, create a ._pth file with the same
name as the DLL (python37._pth) or the executable (python._pth) and
specify one line for each path to add to sys.path. The file based on
the DLL name overrides the one based on the executable, which allows
paths to be restricted for any program loading the runtime if desired.
When the file exists, all registry and environment variables are
ignored, isolated mode is enabled, and site is not imported unless one
line in the file specifies import site.
Using Python on Windows - Finding Modules

How do I run python file without path?

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.

For Pylint, is it possible to have a different pylintrc file for each Eclipse project?

I saw I can change it per Eclipse instance using this solution.
I would like to set it per project. Is it possible?
This is not Eclipse specific, but it may help anyway. According to pylint command line options:
You can specify a configuration file on the command line using the --rcfile option. Otherwise, Pylint searches for a configuration file in the following order and uses the first one it finds:
pylintrc in the current working directory
.pylintrc in the current working directory
If the current working directory is in a Python module, Pylint searches up the hierarchy of Python modules until it finds a pylintrc file. This allows you to specify coding standards on a module-by-module basis. Of course, a directory is judged to be a Python module if it contains an __init__.py file.
The file named by environment variable PYLINTRC
If you have a home directory which isn’t /root:
.pylintrc in your home directory
.config/pylintrc in your home directory
/etc/pylintrc
Points 1 or 3 above may help.
Simply put a configuration file named pylintrc (with no '.') in the root directory of each project. Pylint is run with the project directory as the current working directory, and this is the first place that pylint looks for a configuration file.
The PyDev documentation suggests naming the file .pylintrc, but this is less robust because ./.pylintrc is almost last in the order of places that pylint will search. The PyDev documentation further says that this only works for PyDev 2.8.0 and above, but I am using PyDev 2.6.0 and it works for me.
The search order for the configuration file in the other answer is not quite right. I just submitted an enhancement to the Pylint documentation that describes the configuration file search order correctly:
You can specify a configuration file on the command line using the
--rcfile option. Otherwise, Pylint searches for a configuration file in the following order and uses the first one it finds:
pylintrc in the current working directory
If the current working directory is in a Python module, Pylint
searches up the hierarchy of Python modules until it finds a
pylintrc file. This allows you to specify coding standards on a
module-by-module basis. Of course, a directory is judged to be a
Python module if it contains an __init__.py file.
The file named by environment variable PYLINTRC
.pylintrc in your home directory, unless you have no home directory
or your home directory is /root
.pylintrc in the current working directory
/etc/pylintrc
Note that the configuration file only applies to Python files that are in modules. Thus, Pylint still uses its default rules when analyzing Python files in a directory with no __init__.py file. Perhaps it is this problem that is fixed in PyDev 2.8.0.
For example, I have a bin/ directory containing command line applications. Ordinarily, this directory needs no __init__.py file because it is never imported. I had to add a bin/__init__.py file to get Pylint to analyze these Python files using my pylintrc file.

How to import modules from alternate locations when using Python IDLE?

I've been trying to figure this out for more than 2 days, screening the internet and the tutorial, but yet I don't have solved my problem. I'm a real newb and don't yet really know what I'm doing..
Software I use:
Mac OS X 10.6
Python v3.2.2
Interactive interpreter (IDLE)
Problem:
IDLE's default directory is /Users/ME/Documents/. Files with the extention .py can only be opened when located in this directory. However, I made a folder where I would like to save all the .py files etc that have to do with this software. Currently, IDLE cannot load .py files from the chosen directory by me.
What I did first was I added to IDLE:
import sys.
sys.path.append('Users/Mydir/')
sys.path
However, in an already existing thread from 2010 I read sys.path is for the Interpreter ONLY, and that if I am to change this I need to modify the PYTHONPATH environment variable:
PYTHONPATH="/Me/Documents/mydir:$PYTHONPATH"
export PYTHONPATH
However, I'm confused how to use this and cannot find answers to my following questions:
1) PYTHONPATH (.py?) is already existing on my computer when I installed the program?
If YES, where is it? I cannot find it anywhere.
If NO, I need to create one. But where and what should be the content so that IDLE can load files from a non-default directory? Should it contain only the words in bold?
I hope I made my problem clear.
Cheers
It's not totally clear to me what you mean by load. That could mean Open and Close files in the IDLE editor. Or it could mean being able to use the Python import statement to load existing Python modules from other files. I'll assume the latter, that by load you mean import.
There are two general ways to launch IDLE on Mac OS X. One is from the command line of a terminal session; if you installed Python 3.2 using the python.org installers, by default typing /usr/local/bin/idle3.2 will work. The other way is by launching IDLE.app from /Applications/Python 3.2, i.e. by double-clicking its icon. Because you say the default directory for files is your Documents folder, I'm assuming you are using the second method because IDLE.app sets Documents as its current working directory, which becomes the default directory for *Open*s and *Save*s and is automatically added as the first directory on Python's sys.path, the list of directories that Python uses to search for modules when importing.
If you want to add other directories to sys.path, as you've noted you can use the PYTHONPATH environment variable to do so. The standard way to do this is to add an export PYTHONPATH=... definition to a shell startup script, like .bash_profile. However, if you use IDLE.app, no shell is involved so commands in .bash_profile have no effect.
While there are ways to modify the environment variables for OS X GUI apps, in this case, a simpler solution is to use the other method to invoke IDLE, from the command line of a shell session, using either /usr/local/bin/idle3.2 or, if you've run the Update Shell Profile command in the /Applications/Python 3.2 folder (and opened a new terminal session), just idle3. Then, a PYTHONPATH environment variable you set up will be inherited by that IDLE.
BTW, there is no direct way to modify the initial current working directory of IDLE.app from Documents other than modifying the code in IDLE. If you start IDLE from a command
line, it inherits the current working directory of the shell.
[UPDATE] But rather than fooling around with defining PYTHONPATH, here is another even simpler, and probably better, approach that should work with either IDLE.app or the command line idle. It takes advantage of Python path configuration (.pth) files and user site-package directories. Assuming you are using a standard Python framework build of 3.2 (like from a python.org installer) on Mac OS X, create a path file for the directory you want to permanently add to sys.path. In a terminal session:
mkdir -p ~/Library/Python/3.2/lib/python/site-packages
cd ~/Library/Python/3.2/lib/python/site-packages
cat >my_paths.pth <<EOF
/Users/YOUR_USER_NAME/path/to/your_additional_python_directory_1
/Users/YOUR_USER_NAME/path/to/your_additional_python_directory_2
EOF
Now, whenever you run that Python 3.2 or IDLE under your user name, the directories you have added to the .pth file will automatically be added to sys.path.
BTW, the exact path location of the user site-packages directory for versions of Python earlier than 3.2 or 2.7 may be slightly different. Also, on other Unix-y systems, the default location for the user site-package directory is ~/.local/lib/python3.2/site-packages.
PYTHONPATH is an environment variable (see here and here). I don't have a Mac, but from the threads I have linked to you would type something like
launchctl setenv PYTHONPATH=/Me/Documents/mydir:$PYTHONPATH
on the command line to allow you to run Python scripts from /Me/Documents/mydir. Alternatively, put this line in a file called .bashrc in your home directory (~) and this path will be set each time each time you open a terminal. See here for a short introduction to .bashrc and other .bash* files. Hope that helps.
EDIT See also this question.

Categories

Resources