Swift 3: No such module 'os.log' - swift

I was trying to compile a swift 3 file which contains nothing but:
import Cocoa
The compiler output was:
<unknown>:0: error: missing required module 'os.log'
So I edited the file to be:
import os.log
import Cocoa
And now the compiler output is:
test.swift:1:8: error: no such module 'os.log'
I suspect that the compiler is having trouble finding the legitimate module os.log. I should note that I'm editing the file in VIM, and the same program works fine in my Swift playground.

You need this declaration:
import os
source

Related

VSC Remix extension not being able to compile files with import statements

My Remix VSC extension is not able to compile any contract that has an import statement. This happens even if the imports are local files (located in the same folder than the compiling contract). This is a sample of the contract I try to compile with its import statement:
pragma solidity 0.4.24;
import "./Bank.sol";
contract WETH9 is ERC20Like {
function deposit() public payable;
}
I am able to compile it with the Remix Desktop IDE, though, but for the VSC extension I just get this error:
public\contracts\Setup.sol:3:1: ParserError: Source "Bank.sol" not found: Deferred import import "./Bank.sol"; ^------------------^
Am I missing any kind of setup? I have not found anything regarding this matter.

compiled Python3 module produces error "dynamic module does not define module export function"

I am trying to compile a Python package that I recently migrated from Python2 to Python3.
When running the source code in Python3 it works as expected, so do the source and compiled versions for Python2, but when I cythonize and compile the Python3 package the resulting binaries throw this error when importing a certain module:
dynamic module does not define module export function (PyInit_NKPD)
EDIT:
When I then close the Python interpreter, open it again and import the same model it works.
Interestingly when I put the code on my local drive it imports fine, but when I put it on my server and import it from there I get the above error. And to make matters worse, sometimes it imports fine from the server as well if I just wait a while and try again:
lockjaw:controller frank$ python3.7
Python 3.7.7 (v3.7.7:d7c567b08f, Mar 10 2020, 02:56:16)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import common
added to sys.path: /opt/ohufx/transfer/NUBRIDGE_COMPILE/osx/NKPD/src
trying to import model.NukepediaDB...
NukepediaDB imported
trying to import model.NKPD...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "src/controller/common.py", line 15, in init controller.common
import model.NKPD # this does not import
ImportError: dynamic module does not define module export function (PyInit_NKPD)
>>> import common
added to sys.path: /opt/ohufx/transfer/NUBRIDGE_COMPILE/osx/NKPD/src
trying to import model.NukepediaDB...
trying to import model.NKPD...
NKPD imported
model.NKPD always seems to import fine, but model.NKPD seems to be the stumbling block, even though this may be a red herring. It almost behaves like sometimes the server connection drops out after the first import, though in that case the other incarnations (Python 2/source code) would throw errors as well (not to mention a whole lot of other things would break in my office).
I have reduced the package to three modules with nothing but import and print statements which can be found here (including the resuling C++ and binary files).
The structure is very simple:
Where common is the module I import to test by cd-ing into the src/controller folder, opening Python3.7 and importing common. Common then imports model.NukepediaDB and model.NKPD, the latter causing the trouble.
I cythonized the python files with this line:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 py2cpp.py
Then compiled with this:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 setup.py build_ext --inplace
I would love to understand why the import sometimes fails when run from the server, as I have to distribute this package to others and need to ensure it runs reliably.
Thanks,
Frank

importing a module in Swift

I'm kicking the tires of Swift a little. I'm not using Xcode. I just have a simple .swift file and #!/usr/bin/env swift at the top of the file.
In the file, I have import Blahblah and a file called Blahblah in the same directory.
I get error: no such module 'Blahblah' however when running the .swift file.
How do I import the swift module? Is there something special I need to do?

How to test a class in a Swift package?

I'm currently trying to understand the mechanism of importing dependencies in a Swift package and I've run into an issue with tests. Hope someone can explain what I'm doing wrong. I'm going to describe the problem step-by-step so that you could easily reproduce it.
So I'm creating a new Swift package using swift package init --type executable. This command creates the basic Swift package structure:
Artems-MacBook-Pro:SwiftExample artem$ swift package init --type executable
Creating executable package: SwiftExample
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/SwiftExample/main.swift
Creating Tests/
Creating Tests/LinuxMain.swift
Creating Tests/SwiftExampleTests/
Creating Tests/SwiftExampleTests/SwiftExampleTests.swift
Creating Tests/SwiftExampleTests/XCTestManifests.swift
The package itself is called SwiftExample. As you can see the command also creates an example of a unit test case (SwiftExampleTests.swift).
Then I create a simple class called Car.swift and put it into the Sources/SwiftExample/Classes/ directory:
// Sources/SwiftExample/Classes/Car.swift
class Car {
init() {
print("I'm a car!")
}
}
In the main.swift file I can create an instance of the Car class and everything works pretty much fine:
// Sources/SwiftExample/main.swift
print("Hello, world!")
let car = Car()
The output would be:
Hello, world!
I'm a car!
But the problem is I cannot use this class in my test file. For example, I'm trying to create an instance of the Car class in the testExample() function of the SwiftExampleTests.swift file:
import XCTest
import class Foundation.Bundle
#testable import SwiftExample
final class SwiftExampleTests: XCTestCase {
func testExample() throws {
let car = Car()
<other code goes here>
}
<other code goes here>
}
As you can see I've imported the module itself using the keyword #testable. But when I run swift test command I'm getting this weird error:
Compile Swift Module 'SwiftExample' (2 sources)
Compile Swift Module 'SwiftExampleTests' (2 sources)
Linking ./.build/x86_64-apple-macosx10.10/debug/SwiftExample
/Users/artem/Playgrounds/SwiftExample/Tests/SwiftExampleTests/SwiftExampleTests.swift:9:13: warning: initialization of immutable value 'car' was never used; consider replacing with assignment to '_' or removing it
let car = Car()
~~~~^~~
_
Linking ./.build/x86_64-apple-macosx10.10/debug/SwiftExamplePackageTests.xctest/Contents/MacOS/SwiftExamplePackageTests
Undefined symbols for architecture x86_64:
"_$S12SwiftExample3CarCACycfC", referenced from:
_$S17SwiftExampleTestsAAC04testB0yyKF in SwiftExampleTests.swift.o
"_$S12SwiftExample3CarCMa", referenced from:
_$S17SwiftExampleTestsAAC04testB0yyKF in SwiftExampleTests.swift.o
ld: symbol(s) not found for architecture x86_64
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
error: terminated(1): /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-build-tool -f /Users/artem/Playgrounds/SwiftExample/.build/debug.yaml test output:
I'm certainly doing something wrong here but I can't find any information on the matter in the official docs. Does somebody know what's happening here and how to fix that?
Apparently, the issue won't reproduce anymore in the latest Swift version. The Car class can be imported and the swift test command won't fail.
For the record, my current swift --version output is:
swift-driver version: 1.45.2 Apple Swift version 5.6.1 (swiftlang-5.6.0.323.66 clang-1316.0.20.12)
Target: arm64-apple-macosx12.0

Swift REPL: how to import, load, evaluate, or require a .swift file?

In the Swift REPL, how to import (a.k.a. load, evaluate, require) a typical text *.swift file?
I want to use the code from this file: ~/src/Foo.swift
Syntax like this doesn't work: import ~/src/Foo.swift
For comparison:
An equivalent solution in the Swift REPL for a framework is: import Foundation
An equivalent solution in the Ruby REPL for a *.ruby file is: require "~/src/foo"
These are similar questions that are /not/ what I'm asking:
How to use/make a Swift command-line script, executable, module, etc.
How to use/make an XCode playground, project, library, framework, etc.
How to launch the REPL with a pre-existing list of files.
You need to use -I, so if your modulename.swiftmodule file is in ~/mymodules than launch swift with
swift -I ~/mymodules
and then you will be able to import your module
import module name
Should be that easy
In swift,you can't import a typical *.swift file.
For Import Declaration can only be the following syntax:
“
‌ import-declaration → attributesopt import import-kindopt import-path
import-kind → typealias| struct| class| enum| protocol| var| func
‌ import-path → import-path-identifier| import-path-identifier.import-path
‌ import-path-identifier → identifier| operator”
From: Apple Inc. “The Swift Programming Language (Swift 2)”。 iBooks.
which can be described as these formats:
import module
import import kind module.symbol name
import module.submodule
import head.swift is incompatible with import import-kind module.symbol-name
Usually compile the files you want to import as a framework.Then it can be regarded as a module. use -F framework_directory/ to specify 3rd-party frameworks' search path.
Create a file. For example:
// test.swift
import headtest
print("Hello World")
open your terminal
goto the directory where you create the file.
execute command line
swift -F headtest test.swift
And done.
Simply insert the shebang line at the top of your script:
#!/usr/bin/env xcrun swift
You can copy/paste the source code into the repl and execute it.
Not ideal, obviously, but sometimes useful.
Now Swift REPL supports packages. We can do this by the following steps:
In Xcode, select menu File > New > Package. Choose a name for the package, for example MyLibrary.
Copy your codes or .swift files to the Sources/MyLibrary/ directory in your package.
Remember to make your interface public.
In the command line, go to the package directory and run REPL
Like this
cd MyLibrary/
swift run --repl
In the REPL, import your library
Like this
import MyLibrary
Now you can your codes in the REPL.
It looks like it's not possible to import file and get Xcode to use REPL for it using specifications you gave. I think you can still do it creating a proper framework, but it's not exactly what you was looking.