Found 10784 Articles for Python

What is the difference between dir(), globals() and locals() functions in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:56:48

520 Views

locals() returns you a dictionary of variables declared in the local scope while globals() returns you a dictionary of variables declared in the global scope. At global scope, both locals() and globals() return the same dictionary to global namespace. To notice the difference between the two functions, you can call them from within a function. For example, def fun():     var = 123     print "Locals: ", locals()     print "Vars: ", vars()     print "Globals: ", globals() fun()This will give the output:Locals:  {'var': 123} Globals:  {'__builtins__': , '__name__': '__main__', 'fun': , '__doc__': None, '__package__': None}vars() ... Read More

What is a namespace in Python?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:20

2K+ Views

Namespace is a way to implement scope. In Python, each package, module, class, function and method function owns a "namespace" in which variable names are resolved. When a function,  module or package is evaluated (that is, starts execution), a namespace is created. Think of it as an "evaluation context". When a function, etc., finishes execution, the namespace is dropped. The variables are dropped. Plus there's a global namespace that's used if the name isn't in the local namespace.Each variable name is checked in the local namespace (the body of the function, the module, etc.), and then checked in the global ... Read More

How to set your python path on Windows?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:44:06

800 Views

To set the PYTHONPATH on windows to point Python to look in other directories for module and package imports, go to:My Computer > Properties > Advanced System Settings > Environment VariablesThen under system variables edit the PythonPath variable. At the end of the current PYTHONPATH, add a semicolon and then the directory you want to add to this path:C:\Python27;C:\fooIn this case, are adding the foo directory to the PYTHONPATH. Note that we are appending it and not replacing the PYTHONPATH's original value. In most cases, you shouldn't mess with PYTHONPATH. More often than not, you are doing it wrong and ... Read More

How to set your python path on Mac?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:44:46

1K+ Views

To set the PYTHONPATH on Mac OS to point Python to look in other directories for module and package imports, export the PYTHONPATH variable as follows:$ export PYTHONPATH=${PYTHONPATH}:${HOME}/foo In this case are adding the foo directory to the PYTHONPATH. Note that we are appending it and not replacing the PYTHONPATH's original value. In most cases, you shouldn't mess with PYTHONPATH. More often than not, you are doing it wrong and it will only bring you trouble

How to set your python path on Linux?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:45:28

798 Views

To set the PYTHONPATH on Linux to point Python to look in other directories for the module and package imports, export the PYTHONPATH variable as follows:$ export PYTHONPATH=${PYTHONPATH}:${HOME}/fooIn this case, are adding the foo directory to the PYTHONPATH. Note that we are appending it and not replacing the PYTHONPATH's original value. In most cases, you shouldn't mess with PYTHONPATH. More often than not, you are doing it wrong and it will only bring you trouble.

How to set python environment variable PYTHONPATH on Windows?

Rajendra Dharmkar
Updated on 02-May-2023 12:43:46

34K+ Views

On Windows, you can set the PYTHONPATH environment variable to specify the directories that Python should search for modules when importing them. Here are several ways to set the PYTHONPATH environment variable on Windows Set PYTHONPATH using Command Prompt You can set the PYTHONPATH environment variable using Command Prompt by entering the following command − $set PYTHONPATH=c:\path\to\my\modules This sets the PYTHONPATH environment variable to c:\path\to\my\modules. To make this change permanent, you need to add it to the system environment variables − Open the Start menu and search for "Environment Variables". Click on "Edit the system environment variables". Click on ... Read More

How to set python environment variable PYTHONPATH on Mac?

Rajendra Dharmkar
Updated on 02-May-2023 12:42:27

9K+ Views

To set the Python environment variable PYTHONPATH on a Mac, you can follow these steps − Open the Terminal app on your Mac. Navigate to your home directory by typing cd ~ and pressing Enter. Open the .bash_profile file in a text editor by typing open -e .bash_profile and pressing Enter. Create a new file called .bash profile by typing touch .bash_profile and pressing Enter. Add a line to set the PYTHONPATH environment variable in the file. For example − $export PYTHONPATH=/path/to/my/python/ This sets the PYTHONPATH environment variable to the path /path/to/my/python/modules. You should replace this with the path to the directory where your Python modules are ... Read More

How to set Python environment variable PYTHONPATH on Linux?

Rajendra Dharmkar
Updated on 02-May-2023 12:39:55

16K+ Views

To set the PYTHONPATH environment variable on Linux, follow these steps − Open a terminal window on your Linux system. Determine the path to your Python module or package. For example, suppose you have a Python module named mymodule located in the /home/user/myproject folder. Set the PYTHONPATH environment variable to the path of your module or package using the following command − $export PYTHONPATH=/home/user/myproject:$ This command sets the PYTHONPATH environment variable to /home/user/myproject and also includes the previous value of PYTHONPATH in case it was already set. Note that the path should be separated by a colon (:) on Linux. Verify that the PYTHONPATH environment variable has been set correctly using the following command − ... Read More

What is PYTHONPATH environment variable in Python?

Rajendra Dharmkar
Updated on 26-Aug-2023 08:50:16

36K+ Views

In Python, PYTHONPATH is an environment variable that specifies a list of directories to search for Python modules when importing them. When you import a module in Python, Python looks for the module in the directories specified in sys.path, which is a list of directories that includes the current working directory and directories specified in PYTHONPATH. PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. For most installations, you should not set these variables since they are not needed for Python to run. Python knows where to find ... Read More

What is the use of "from...import *" Statement in Python?

Rajendra Dharmkar
Updated on 30-Sep-2019 08:48:52

1K+ Views

The "from module import *" statement is used to import all function from a Python module. For example, if you want to import all functions from math module and do not want to prefix "math." while calling them, you can do it as follows:>>> from math import * >>> sin(0) 0.0 >>> cos(0) 1.0Note that for any reasonable large set of code, if you import * you will likely be cementing it into the module, unable to be removed. This is because it is difficult to determine what items used in the code are coming from 'module', making it easy ... Read More

Advertisements