Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables imported from the module becomes $null, after the same module imported again

I have a simple module: varExp.psm1

$var1 = 20

Export-ModuleMember -Variable var1

And I import this module into PS session:

PS> Import-Module .\varExp.psm1

then

PS> $var1

20

But after I import it second time

PS> Import-Module .\varExp.psm1

PS> $var1

PS>

$var1 becomes null...

Anybody knows what is going on here? (PS2.0)

Edit: There are workarounds: Forcing reloading with Import-Module .\varExp.psm1 -Force, and testing if module was loaded before: if(-not (Get-Module varExp)) { Import-Module .\varExp.psm1 }. But I was hoping to get some reason behind $null value in simple case.

like image 817
iank Avatar asked Nov 03 '11 20:11

iank


People also ask

What happens if I import the same module twice?

If multiple modules imports the same module then angular evaluates it only once (When it encounters the module first time). It follows this condition even the module appears at any level in a hierarchy of imported NgModules.

Can we import a module twice?

The rules are quite simple: the same module is evaluated only once, in other words, the module-level scope is executed just once. If the module, once evaluated, is imported again, it's second evaluation is skipped and the resolved already exports are used.

What happens when a package is imported?

When you import a package, the sequence of steps is the same as when you import a module. The only difference is that the packages's code (i.e., the code that creates the "module code object") is the code of the package's __init__.py .

What happens when you import a module Python?

When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.


2 Answers

I don't know exactly what happens, but when you are re-importing a module , you have to use -Force.

When you include -verbose you can see the difference between the two:

PS> import-module .\test -Verbose
VERBOSE: Importing variable 'var1'.

With force:

PS> import-module .\test -Verbose -Force
VERBOSE: Removing the imported "var1" variable.
VERBOSE: Loading module from path 'C:\test\test.psm1'.
VERBOSE: Importing variable 'var1'.

The documentation does say the following:

If you import members with the same name and the same type into your session, Windows PowerShell uses the member imported last by default. Variables and aliases are replaced, and the originals are not accessible.

I think the originals are not accessible means in this $var1 is not accessible when you re-import the module.

like image 158
manojlds Avatar answered Oct 07 '22 15:10

manojlds


For me @manojlds gives the interesting part of the answer.

Here are some more observations that can help @iank to sleep next night :

when you first load the module you can use the following commande

PS> $a = Import-Module .\varExp.psm1 -PassThru
PS> $a.ExportedVariables.var1
Name                           Value
----                           -----
var1                           20

Now you change the file .\varExp.psm1 to add a new var $var2=30 and also export it. If you stay in the same powershell you can test. $var2 does not appear.

PS> $b = Import-Module .\varExp.psm1 -PassThru
PS> $b.ExportedVariables
Name                           Value
----                           -----
var1                           

For me as you do not remove the module (Remove-Module varexp) the module is reloaded from the memory information and vars are really replace but with nothing. If you remove the module or use -Force the module is reloaded from file. Try the following :

PS> import-module .\varExp.psm1
PS> Remove-Variable var1
PS> import-module .\varExp.psm1
PS> Get-ChildItem variable:
...
var1
...

$Var1 is recreated but not assign.

like image 23
JPBlanc Avatar answered Oct 07 '22 16:10

JPBlanc