How include a file module in Julia 1.7? - julia

My goal is: in Julia 1.7 on Mac with M1 processor I would include a module file with many functions inside.
I've tried to follow this thread to generate my own package
but it's generated an error.
I followed the answer but when i try importing MyPackage, Julia says: ArgumentError: Package MyPackage not found in current path.
With pwd() current path is Users/myname and in this folder MyPackage exists.
With command "import MyPackage" where i can see default folder for packages import ?
Where I get wrong ?
Sorry for my English.

I know two options here. Suppose I generated a package with the following file structure
MyPackage/
src/
MyPackage.jl
Project.toml
and MyPackage.jl is
module MyPackage
export greet
greet() = print("Hello World!")
end # module
Then there are two options, I know
Including of module MyPackage;
Embedding of package MyPackage into Julia's ecosystem.
Suppose, that pwd is MyPackage/
First option is
julia> include("src/MyPackage.jl")
Main.MyPackage
julia> using .MyPackage
julia> greet()
Hello World!
Notice the dot in using statement.
Second option
Start Julia REPL and enter pkg mode, then
(#v1.6) pkg> dev MyPackage/
Resolving package versions...
Updating `~/.julia/environments/v1.6/Project.toml`
[88e94d31] + MyPackage v0.1.0 `foo/bar/MyPackage`
Updating `~/.julia/environments/v1.6/Manifest.toml`
[88e94d31] + MyPackage v0.1.0 `foo/bar/MyPackage`
(#v1.6) pkg> st
Status `~/.julia/environments/v1.6/Project.toml`
[6e4b80f9] BenchmarkTools v1.3.1
[0772a1fa] CubicEoS v0.2.0
..........
[88e94d31] MyPackage v0.1.0 `foo/bar/MyPackage`
..........
[09ab397b] StructArrays v0.6.5
[a759f4b9] TimerOutputs v0.5.15
st command says that MyPackage became available in default environment.
Then you should be able to import/using the package. For the first time you should see precompiling message.
julia> using MyPackage
[ Info: Precompiling MyPackage [88e94d31-ecaf-41ca-ae10-053d89a189ff]
julia> greet()
Hello World!
P.S. The best place for local packages is .julia/dev/.

Related

How to install a series of Julia packages from a file

In Python, if I install mambaforge or conda then I can make a file with extension .yml and then inside it list the name of packages I want to install alongside their specific versions. How can I do a similar way of installing packages in Julia?
I understand that if I have already installed Julia packages by addcommand in package manager, then I have a file named Project.toml which I can use to install the same packages later. However, this still does not look as good as Python's way of installing packages.
Upon further investigation I realized that to install Julia packages from an empty Prokect.tomlfile, I should add [deps]in the file followed by the name of packages I want and then give each package a uuidwhich can be found here. For example:
[deps]
Images = "916415d5-f1e6-5110-898d-aaa5f9f070e0"
After all , this is still tedious as it needs to find all those uuids.
How can I install packages in Julia the same way I described for Python?
Is there a particular reason that you want to write package names to a .yml file and then read the packages from there? After all, you can generate the Project file and add multiple dependencies automatically:
(#v1.8) pkg> generate MyProject # or whatever name you like
(#v1.8) pkg> activate MyProject
(MyProject) pkg> add Countries Crayons CSV # some example packages
(In recent versions of Julia, an installation prompt will appear if a package isn't already installed).
Speaking from experience, learning to use environments in Julia can be challenging to a new user, but rewarding! The documentation for Pkg.jl are helpful here.
If you are just assembling an environment for your own code, there is probably no need for you to manually edit Project.toml. On the other hand, if you are maintaining a package, you might wish to edit the file directly (e.g., for specifying compatability).
Maybe you can use this:
using TOML
using HTTP
using DataStructures
## Get the Registry.toml file if it already doesn't exist in the current directory
function raed_reg()
if !isfile("Registry.toml")
HTTP.download(
"https://raw.githubusercontent.com/JuliaRegistries/General/master/Registry.toml",
"Registry.toml"
)
end
reg = TOML.parsefile("Registry.toml")["packages"]
return reg
end
## Find the UUID of a specific package from the Registry.toml file
function get_uuid(pkg_name)
reg = raed_reg()
for (uuid, pkg) in reg
if pkg["name"] == pkg_name
return uuid
end
end
end
## Create a dictionary for the gotten UUIDs, by setting the name = UUID and convert it to a project.toml file
function create_project_toml(Pkgs::Vector{String})
reg = raed_reg()
pkgs_names_uuid = OrderedDict{AbstractString, AbstractString}()
pkgs_names_uuid["[deps]"] = ""
for pkg in Pkgs
_uuid = get_uuid(pkg)
pkgs_names_uuid[pkg] = _uuid
end
open("project.toml", "w") do io
TOML.print(io, pkgs_names_uuid)
end
end
## Test on the packages "ClusterAnalysis" and "EvoTrees"
create_project_toml(["ClusterAnalysis", "EvoTrees"])

Expected the file `src/HijriConverter.jl` to exist for package `HijriConverter` at `…/HijriConverter.jl`

I am trying to add dependencies to a package that I am developing, but I get the following error:
(HijriConverter) pkg> add Parameters
Updating registry at `~/.julia/registries/General.toml`
Resolving package versions...
ERROR: expected the file `src/HijriConverter.jl` to exist for package `HijriConverter` at `/home/jafar_isbarov/Documents/projects/hijri/hijri-converter-julia-package/HijriConverter.jl`
Adding a file named HijriConverter to the src folder does solve the problem. I have two questions.
(1) Why do I need a file with the same name as the package? Is it supposed to have certain content?
(2) If I add that file, add dependencies, and delete the file afterwards, will that cause any problems?
Thanks.
To understand the minimal package requirements try running Pkg.generate as in code below:
julia> Pkg.generate("MyPackage")
Generating project MyPackage:
MyPackage\Project.toml
MyPackage\src/MyPackage.jl
Dict{String, Base.UUID} with 1 entry:
"MyPackage" => UUID("bad70bc4-3cf3-42bb-9f14-6b74ac64e2d7")
And here is the minimal file layout:
shell> tree /F
C:.
└───MyPackage
│ Project.toml
│
└───src
MyPackage.jl
And this is the generated module content with a single simple function:
shell> more MyPackage\\src\\MyPackage.jl
module MyPackage
greet() = print("Hello World!")
end # module
Further reading: https://pkgdocs.julialang.org/v1/creating-packages/

Julia#1.0.0: How to get installed Pkg.uuid in REPL

I want to use Base.compilecache.
It needs PkgId(UUID). But I don't know how to get installed Pkg.uuid in REPL.
https://docs.julialang.org/en/v1.0.0/base/base/#Base.compilecache
I tried Pkg.PackageSpec but can't get UUID.
https://docs.julialang.org/en/stable/stdlib/Pkg/#Pkg.PackageSpec
julia> using Pkg
julia> Pkg.status()
Status `~/.julia/environments/v1.0/Project.toml`
[8f4d0f93] Conda v1.0.1
[7073ff75] IJulia v1.9.3
julia> Pkg.PackageSpec("Conda")
PackageSpec(name=Conda)
julia> Pkg.PackageSpec("Conda").uuid
UUID("00000000-0000-0000-0000-000000000000")
My goal is to call Base.compilecache at Docker file.
Julia v0.6.4 code
julia -e "Base.compilecache(\"JSON\")"
You can use Base.identify_package("Conda").

Julia : JLD package doesn't work when running Julia development version

The current version of Julia is 0.4.6. I, however, am running the development version 0.5. Suddenly JLD doesn't work. It's installed and updated. Yesterday I compiled code using JLD but this morning it doesn't work.
julia> using JLD
INFO: Precompiling module JLD...
WARNING: Method definition convert(Type{#T<:AbstractString}, AbstractArray{#S<:Union{Char, Int32, UInt32}, 1}) in module Base at unicode/utf32.jl:131 overwritten in module LegacyStrings at /root/.julia/v0.5/LegacyStrings/src/utf32.jl:133.
WARNING: Method definition isvalid(Array{Char, 1}) in module Base at unicode/utf32.jl:177 overwritten in module LegacyStrings at /root/.julia/v0.5/LegacyStrings/src/utf32.jl:179.
WARNING: New definition
string(Union{Char, LegacyStrings.UTF8String, LegacyStrings.ASCIIString}...) at /root/.julia/v0.5/LegacyStrings/src/utf8.jl:161
is ambiguous with:
string(Union{Char, UTF8String, ASCIIString}...) at unicode/utf8.jl:166.
To fix, define
string(Char...)
before the new definition.
WARNING: both LegacyStrings and Base export "UTF16String"; uses of it in module JLD must be qualified
ERROR: LoadError: LoadError: UndefVarError: UTF16String not defined
in include(::ASCIIString) at ./boot.jl:234
in include_from_node1(::ASCIIString) at ./loading.jl:417
in include(::ASCIIString) at ./boot.jl:234
in include_from_node1(::ASCIIString) at ./loading.jl:417
[inlined code] from ./boot.jl:237
in anonymous at ./<no file>:4294967295
in eval(::Module, ::Any) at ./boot.jl:237
[inlined code] from ./sysimg.jl:11
in process_options(::Base.JLOptions) at ./client.jl:239
in _start() at ./client.jl:318
while loading /root/.julia/v0.5/JLD/src/jld_types.jl, in expression starting on line 11
while loading /root/.julia/v0.5/JLD/src/JLD.jl, in expression starting on line 130
ERROR: Failed to precompile JLD to /root/.julia/lib/v0.5/JLD.ji
in error(::ASCIIString) at ./error.jl:21
in compilecache(::ASCIIString) at ./loading.jl:496
in compilecache(::Symbol) at ./loading.jl:485
in require(::Symbol) at ./loading.jl:355
in eval(::Module, ::Any) at ./boot.jl:237
When using the development version of Julia, you need to use the development versions of the packages (which works for packages where the developer keeps master up-to-date but hasn't tagged yet). If you run Pkg.checkout("JLD") to checkout master, JLD should work (works on my machine. Note you may need to Pkg.update() before checking out, and you may need to quit Julia and re-open it to recompile the new version).
But as a word of caution, don't use the development versions of Julia as a way to test things out. Remember, the language is still in alpha and there is no guarantee that the package ecosystem or Julia itself will work with the daily master. The dailies are good for working on the language and preparing packages for the next version (and being ballsy I guess).
Well, you're using Julia 0.5 which is still in development. If you switch to 0.4.6 then it should be fine. I'm using that and JLD works fine for me. You could also trying running Pkg.update() closing Julia, then reopening, to see if that helps.

Change Package directory in Julia

I want to change the Package directory in Julia. The default is
"~/.julia/v0.4"
I want to move it to /opt/julia/v0.4/. Ideally I want to move the packages that are already installed in ~/.julia/v0.4 to the new location. But if that is not possible I can reinstall them.
What do I have to do?
Julia-v0.6 and before
one can change julia's package directory by following these steps:
run export JULIA_PKGDIR=/your/directory in shell(or manually add a new environment variable JULIA_PKGDIR on windows)
run Pkg.init() in julia to initialize a new package system
copy REQUIRE from old directory to the new one
run Pkg.resolve() in julia
Julia-v0.7+
The "Package directory" in the new package manager is called DEPOT_PATH, it can be changed by adding an environment variable JULIA_DEPOT_PATH:
JULIA_DEPOT_PATH=./test julia
julia> DEPOT_PATH
1-element Array{String,1}:
"./test"
(v0.7) pkg> add JSON2
Cloning default registries into /Users/gnimuc/test/registries
With the new package manager, we are able to create isolated projects in any directory we want instead of having a single giant package directory. Every project contains a Project.toml and a Manifest.toml if it has some dependencies. These two files record and keep tracking the environment of the project.
UPDATE
The following info might be obsoleted. I highly recommend to use PkgTemplates.jl for generating projects in Julia-v1.0+.
Generate a new project
We can generate a new project in any folder, but we must cd to the project folder to using the project. The (v0.7) below shows we're still in the default environment, so we cannot use the newly generated project:
(v0.7) pkg> generate ./MyNewProject
Generating project MyNewProject:
./MyNewProject/Project.toml
./MyNewProject/src/MyNewProject.jl
julia> using MyNewProject
ERROR: ArgumentError: Module MyNewProject not found in current path.
Run `Pkg.add("MyNewProject")` to install the MyNewProject package.
Stacktrace:
[1] require(::Module, ::Symbol) at ./loading.jl:868
If we cd to the project folder and activate the environment, then we can using our new project without any problems:
shell> cd MyNewProject/
/Users/gnimuc/MyNewProject
(v0.7) pkg> activate .
(MyNewProject) pkg>
julia> using MyNewProject
I think that's the big difference between the new package manager and the old one. In short, we need to explicitly activate our unregistered project/package.
Download and init someone else's project
According to the doc, we can add an unregistered package/project via add command:
(HelloWorld) pkg> add https://github.com/fredrikekre/ImportMacros.jl
This command adds the target package as a dependency of our current project. In this example, we added ImportMacros in HelloWorld's Manifest.toml. What if we just use it as a top-level project? To add it to the default environment (v0.7)? no, we don't need to. The answer is we can directly download the code, cd to the folder and run instantiate in the pkg mode:
shell> git clone https://github.com/Gnimuc/GLTF.jl GLTF
Cloning into 'GLTF'...
remote: Counting objects: 286, done.
remote: Compressing objects: 100% (56/56), done.
remote: Total 286 (delta 73), reused 103 (delta 59), pack-reused 167
Receiving objects: 100% (286/286), 62.16 KiB | 46.00 KiB/s, done.
Resolving deltas: 100% (135/135), done.
shell> cd GLTF
pkg> activate .
(GLTF) pkg> instantiate
Updating registry at `~/.julia/registries/General`
Updating git-repo `https://github.com/JuliaRegistries/General.git`
The new package manager is great! We neither need "include before using" nor make everything as a package just for using it. We have full-featured "Project" now!
Julia only way:
julia> ENV["JULIA_PKGDIR"] = "E:\\Julia-0.6.0\\portable"
"E:\\Julia-0.6.0\\portable"
julia> ENV["JULIA_PKGDIR"]
"E:\\Julia-0.6.0\\portable"
julia> Pkg.init()
INFO: Initializing package repository E:\Julia-0.6.0\portable\v0.6
INFO: Cloning METADATA from https://github.com/JuliaLang/METADATA.jl
However, the cache dir is still pointing to the old folder, so I checked why that is and figured it out:
julia> Base.LOAD_CACHE_PATH
1-element Array{String,1}:
"C:\\Users\\kung\\.julia\\lib\\v0.6"
julia> Pkg.__init__()
2-element Array{String,1}:
"E:\\Julia-0.6.0\\portable\\lib\\v0.6"
"C:\\Users\\kung\\.julia\\lib\\v0.6"
julia> pop!(Base.LOAD_CACHE_PATH)
"C:\\Users\\kung\\.julia\\lib\\v0.6"
julia> Base.LOAD_CACHE_PATH
1-element Array{String,1}:
"E:\\Julia-0.6.0\\portable\\lib\\v0.6"
As simple function:
function set_julia_dir(dir::String)
ENV["JULIA_PKGDIR"] = dir
Pkg.init()
Pkg.__init__()
pop!(Base.LOAD_CACHE_PATH)
end
set_julia_dir("E:\\Julia-0.6.0\\portable")

Resources